Back to index

wims  3.65+svn20090927
Classes | Public Member Functions | Static Public Member Functions | Static Public Attributes | Package Attributes | Static Private Member Functions
WIMSchem.ds.TrivialDOM Class Reference
Collaboration diagram for WIMSchem.ds.TrivialDOM:
Collaboration graph
[legend]

List of all members.

Classes

class  Node
class  Text

Public Member Functions

Node CreateNode (String Name)
Text CreateText (String Text, boolean Preserve)
 TrivialDOM ()
 TrivialDOM (String DocName)
 TrivialDOM (Node DocNode)
Node Document ()
String toString ()

Static Public Member Functions

static TrivialDOM ReadXML (BufferedReader in) throws IOException
static void WriteXML (Writer out, TrivialDOM dom) throws IOException
static String EscapeAttr (String S)
static String EscapeText (String S)

Static Public Attributes

static final int TYPE_NODE = 1
static final int TYPE_TEXT = 2

Package Attributes

Node doc = null

Static Private Member Functions

static String snip (String str)
static void RecursiveWriteNode (Writer out, Node nod, int Level) throws IOException

Detailed Description

Definition at line 25 of file TrivialDOM.java.


Constructor & Destructor Documentation

Definition at line 135 of file TrivialDOM.java.

{}

Here is the caller graph for this function:

WIMSchem.ds.TrivialDOM.TrivialDOM ( String  DocName) [inline]

Definition at line 137 of file TrivialDOM.java.

    {
       doc=new Node(DocName);
    }
WIMSchem.ds.TrivialDOM.TrivialDOM ( Node  DocNode) [inline]

Definition at line 141 of file TrivialDOM.java.

    {
       doc=DocNode;
    }

Member Function Documentation

Node WIMSchem.ds.TrivialDOM.CreateNode ( String  Name) [inline]

Definition at line 128 of file TrivialDOM.java.

{return new Node(Name);}
Text WIMSchem.ds.TrivialDOM.CreateText ( String  Text,
boolean  Preserve 
) [inline]

Definition at line 129 of file TrivialDOM.java.

{return new Text(Text,Preserve);}

Definition at line 146 of file TrivialDOM.java.

{return doc;}

Here is the caller graph for this function:

static String WIMSchem.ds.TrivialDOM.EscapeAttr ( String  S) [inline, static]

Definition at line 385 of file TrivialDOM.java.

    {
       int i;
       while ((i=S.indexOf('"'))>=0) {S=S.substring(0,i)+"""+S.substring(i+1);}
       while ((i=S.indexOf('\''))>=0) {S=S.substring(0,i)+"'"+S.substring(i+1);}
       return S;
    }

Here is the caller graph for this function:

static String WIMSchem.ds.TrivialDOM.EscapeText ( String  S) [inline, static]

Definition at line 393 of file TrivialDOM.java.

    {
       String str="";
       int i;
       while ((i=S.indexOf('&'))>=0) {str=str+S.substring(0,i)+"&"; S=S.substring(i+1);}
       S=str+S;
       while ((i=S.indexOf('<'))>=0) {S=S.substring(0,i)+"&lt;"+S.substring(i+1);}
       while ((i=S.indexOf('>'))>=0) {S=S.substring(0,i)+"&gt;"+S.substring(i+1);}
       return S;
    }

Here is the caller graph for this function:

static TrivialDOM WIMSchem.ds.TrivialDOM.ReadXML ( BufferedReader  in) throws IOException [inline, static]

Definition at line 157 of file TrivialDOM.java.

    {
       final String EOF="ReadXML: unexpected end of file during parsing";

       // PART 1: read the input file one character at a time, and carve it up into chunks, which are preserved as strings; these
       // include tag start & end, text, CDATA, and comments.
       
       ArrayList<String> chunks=new ArrayList<String>();
       String str="";
       while (true)
       {
           int ich;
           if (str.length()==0)
           {
              ich=in.read();
              if (ich<0) break;
              str=String.valueOf((char)ich);
           }
           
           if (str.charAt(0)=='<') // either a tag or a CDATA
           {
              for (int n=0;n<2;n++)
              {
                  ich=in.read();
                  if (ich<0) throw new IOException(EOF);
                  str=str+(char)ich;
              }
              
              if (str.startsWith("<![")) // it's a CDATA
              {
                  while (true)
                  {
                     ich=in.read();
                     if (ich<0) throw new IOException(EOF);
                     str=str+(char)ich;
                     if (str.endsWith("]]>")) 
                     {
                         chunks.add(str);
                         str="";
                         break;
                     }
                  }
              }
              else if (str.startsWith("<!-")) // it's a comment
              {
                  while (true)
                  {
                     ich=in.read();
                     if (ich<0) throw new IOException(EOF);
                     str=str+(char)ich;
                     if (str.endsWith("-->")) 
                     {
                         chunks.add(str);
                         str="";
                         break;
                     }
                  }
              }
              else // it's an opening tag, which will get closed later
              {
                  boolean inquot=false;
                  while (true)
                  {
                     ich=in.read();
                     if (ich<0) throw new IOException(EOF);
                     str=str+(char)ich;
                     if ((char)ich=='"') inquot=!inquot;
                     else if ((char)ich=='>') 
                     {
                         chunks.add(str);
                         str="";
                         break;
                     }
                  }
              }
           }
           else // must be plain text
           {
              boolean eof=false;
              while (true)
              {
                  ich=in.read();
                  if (ich<0) {eof=true; break;}
                  if ((char)ich=='<')
                  {
                     chunks.add(str);
                     str=String.valueOf((char)ich);
                     break;
                  }
                  str=str+(char)ich;
              }
              if (eof)
              {
                  if (str.trim().length()==0) break; else throw new IOException(EOF);
              }
           }
       }

       // PART 2: analyze the resulting pieces, and build up the node tree

       TrivialDOM xml=new TrivialDOM("unknown");
       Node node=null;
       for (int n=0;n<chunks.size();n++)
       {
           str=chunks.get(n);
           if (str.trim().length()==0) continue; // ignore chunks which are pure whitespace

           if (str.charAt(0)=='<' && str.length()>=2 && ((str.charAt(1)>='A' && str.charAt(1)<='Z') ||
                                                    (str.charAt(1)>='a' && str.charAt(1)<='z')) && str.endsWith(">"))
           {
              str=str.substring(1,str.length()-1);
              boolean isclosed=str.endsWith("/");
              if (isclosed) str=str.substring(0,str.length()-1);
              
              String[] bits=str.split(" ");
              Node newNode=null;
              if (node==null)
              {
                  newNode=xml.Document();
                  newNode.SetNodeName(bits[0]);
              }
              else newNode=node.AppendNode(bits[0]);
              
              for (int i=1;i<bits.length;i++)
              {
                  int spc=bits[i].indexOf("=");
                  if (spc<=0) throw new IOException("Malformatted attribute: ["+snip(bits[i])+"].");
                  String key=bits[i].substring(0,spc),val=bits[i].substring(spc+1);
                  if (!val.startsWith("\"") || !val.endsWith("\""))
                     throw new IOException("Malformed attribute value: ["+snip(bits[i])+"].");
                  val=val.substring(1,val.length()-1);
                  newNode.SetAttribute(key,val);
              }
              
              if (!isclosed) node=newNode;
           }
           else if (str.startsWith("</"))
           {
              if (node==null) throw new IOException("Unexpected end tag: ["+snip(str)+"].");
              str=str.substring(2,str.length()-1);
              if (str.compareTo(node.NodeName())!=0)
                  throw new IOException("Closing tag does not match opening tag: ["+snip(str)+"].");
              node=node.Parent();
           }
           else if (str.startsWith("<![CDATA["))
           {
              if (node==null) throw new IOException("Unexpected CDATA node: ["+snip(str)+"].");
              if (!str.endsWith("]]>")) throw new IOException("CDATA node not ended: ["+snip(str)+"].");
              str=str.substring(9,str.length()-3);
              node.AppendText(str,true);
           }
           else if (str.startsWith("<!--"))
           {
              if (!str.endsWith("-->")) throw new IOException("Unterminated comment: ["+snip(str)+"].");
           }
           else if (str.startsWith("<?")) {} // ignore
           else if (str.startsWith("<")) throw new IOException("Unexpected angle bracket, near: ["+snip(str)+"].");
           else
           {
              if (node==null) throw new IOException("Misplaced text-like block: ["+snip(str)+"].");
              node.AppendText(str.trim(),false);
           }
       }      

       return xml;
    }

Here is the call graph for this function:

Here is the caller graph for this function:

static void WIMSchem.ds.TrivialDOM.RecursiveWriteNode ( Writer  out,
Node  nod,
int  Level 
) throws IOException [inline, static, private]

Definition at line 340 of file TrivialDOM.java.

    {
       // emit the node tag & attributes
       
       for (int n=0;n<Level;n++) out.write(" ");
       out.write("<"+nod.NodeName());
       String[] attr=nod.GetAttributeNames();
       for (int n=0;n<attr.length;n++) out.write(" "+attr[n]+"=\""+EscapeAttr(nod.Attribute(attr[n]))+"\"");

       // special case for empty nodes
       if (nod.NumChildren()==0) {out.write("/>\n"); return;}

       out.write(">");
       
       boolean doIndent=true;
       if (nod.NumChildren()==1 && nod.ChildType(0)==TYPE_TEXT) doIndent=false;
       else if (nod.NumChildren()>0 && nod.ChildType(0)==TYPE_TEXT && nod.GetChildText(0).Preserve()) doIndent=false;
       
       if (doIndent) out.write("\n");
       
       // emit the child nodes
       
       for (int n=0;n<nod.NumChildren();n++)
       {
           if (nod.ChildType(n)==TYPE_TEXT)
           {
              Text txt=nod.GetChildText(n);
              if (doIndent) for (int i=0;i<Level+1;i++) out.write(" ");
              if (txt.Preserve()) out.write("<![CDATA[");
              out.write(EscapeText(txt.Get()));
              if (txt.Preserve()) out.write("]]>");
              if (doIndent) out.write("\n");
           }
           else RecursiveWriteNode(out,nod.GetChildNode(n),Level+1);
       }
       
       // emit the closing tag
       
       if (doIndent) for (int n=0;n<Level;n++) out.write(" ");
       out.write("</"+nod.NodeName()+">\n");
    }

Here is the call graph for this function:

Here is the caller graph for this function:

static String WIMSchem.ds.TrivialDOM.snip ( String  str) [inline, static, private]

Definition at line 325 of file TrivialDOM.java.

    {
       if (str.length()<60) return str;
       return str.substring(0,60)+"...";
    }

Here is the caller graph for this function:

String WIMSchem.ds.TrivialDOM.toString ( ) [inline]

Definition at line 147 of file TrivialDOM.java.

    {
       StringWriter out=new StringWriter();
       try {WriteXML(out,this);}
       catch (IOException e) {return e.getMessage();}
       return out.toString();
    }

Here is the call graph for this function:

static void WIMSchem.ds.TrivialDOM.WriteXML ( Writer  out,
TrivialDOM  dom 
) throws IOException [inline, static]

Definition at line 333 of file TrivialDOM.java.

    {
       out.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
       RecursiveWriteNode(out,dom.Document(),0);
       out.flush();
    }

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

Node WIMSchem.ds.TrivialDOM.doc = null [package]

Definition at line 131 of file TrivialDOM.java.

final int WIMSchem.ds.TrivialDOM.TYPE_NODE = 1 [static]

Definition at line 27 of file TrivialDOM.java.

final int WIMSchem.ds.TrivialDOM.TYPE_TEXT = 2 [static]

Definition at line 28 of file TrivialDOM.java.


The documentation for this class was generated from the following file: