Back to index

wims  3.65+svn20090927
Classes | Public Member Functions | Static Public Member Functions | Package Attributes
rene.util.xml.XmlReader Class Reference
Collaboration diagram for rene.util.xml.XmlReader:
Collaboration graph
[legend]

List of all members.

Classes

protocol  String

Public Member Functions

 XmlReader ()
 XmlReader (BufferedReader in)
 XmlReader (InputStream in) throws XmlReaderException
void init (InputStream in) throws XmlReaderException
XmlTree scan () throws XmlReaderException
 Scan an xml file.
void scanContent (XmlTree t) throws XmlReaderException
boolean empty (String s)
int skipBlanks () throws XmlReaderException
 Skip Blanks.
String scanFor (char end) throws XmlReaderException
 Scan for an end character.
String scanFor (String s) throws XmlReaderException
 Scan for a specific string.
String scanTagFor (char end) throws XmlReaderException
 Scan tag for an end character (interpreting " and ')
String scanTagFor (String s) throws XmlReaderException
 Scan tag for a specific string (interpreting " and ')
int read () throws XmlReaderException
boolean found (String s)

Static Public Member Functions

static void main (String args[])
 A test program.
static void print (XmlTree t)
static boolean testXml (String s)

Package Attributes

BufferedReader In
SimpleStringBuffer buf = new SimpleStringBuffer(10000)
String Line = null
int LinePos

Detailed Description

Definition at line 16 of file XmlReader.java.


Constructor & Destructor Documentation

Definition at line 20 of file XmlReader.java.

       {      In=null;
       }

Here is the caller graph for this function:

rene.util.xml.XmlReader.XmlReader ( BufferedReader  in) [inline]

Definition at line 24 of file XmlReader.java.

       {      In=in;
       }
rene.util.xml.XmlReader.XmlReader ( InputStream  in) throws XmlReaderException [inline]

Definition at line 28 of file XmlReader.java.

       {      try
              {      // read the file into a buffer
                     BufferedInputStream rin=new BufferedInputStream(in);
                     SimpleByteBuffer bb=new SimpleByteBuffer(10000);
                     while (true)
                     {      int k=rin.read();
                            if (k<0) break;
                            bb.append((byte)k);
                     }
                     rin.close();
                     byte b[]=bb.getByteArray();
                     
                     // Try to open an ASCII stream, or a default stream
                     ByteArrayInputStream bin=new ByteArrayInputStream(b);
                     XmlReader R=null;
                     try
                     {      R=new XmlReader(new BufferedReader(new InputStreamReader(bin,"ASCII")));
                     }
                     catch (UnsupportedEncodingException ex)
                     {      R=new XmlReader(new BufferedReader(new InputStreamReader(bin)));
                     }             
                                          
                     // Determine the encoding
                     String Encoding=null;
                     while (true)
                     {      while (true)
                            {      int c=R.read();
                                   if (c==-1) throw new Exception("<?xml> tag not found");
                                   if (c=='<') break;
                            }
                            if (R.found("?xml"))
                            {      String s=R.scanFor("?>");
                                   if (s==null) throw new Exception("<?xml> tag error");
                                   int n=s.indexOf("encoding=\"");
                                   if (n>=0)
                                   {      n+="encoding=\"".length();
                                          s=s.substring(n);
                                          int m=s.indexOf('\"');
                                          if (m<0) throw new Exception("Closing bracket missing");
                                          Encoding=s.substring(0,m).toUpperCase();
                                          if (Encoding.equals("UTF-8")) Encoding="UTF8";
                                                 // for IE5 !
                                          break;
                                   }
                                   break;
                            }
                     }
                     
                     // Open a stream with this encoding
                     bin=new ByteArrayInputStream(b);
                     if (Encoding==null)
                            In=new BufferedReader(new InputStreamReader(bin));
                     else
                            try 
                            {      In=new BufferedReader(new InputStreamReader(
                                          bin,Encoding));
                            }
                            catch (UnsupportedEncodingException e)
                            {      try
                                   {      In=new BufferedReader(new InputStreamReader(
                                                 bin,"ASCII"));
                                   }
                                   catch (UnsupportedEncodingException ex)
                                   {      In=new BufferedReader(new InputStreamReader(bin));
                                   }                                  
                            }
              }
              catch (Exception e)
              {      throw new XmlReaderException(e.toString());
              }
       }

Here is the call graph for this function:


Member Function Documentation

Definition at line 247 of file XmlReader.java.

       {      int n=s.length();
              for (int i=0; i<n; i++)
              {      char c=s.charAt(i);
                     if (c!=' ' && c!='\n' && c!='\t') return false;
              }
              return true;
       }

Here is the caller graph for this function:

Returns:
If the string is at the current line position.

Definition at line 399 of file XmlReader.java.

       {      int n=s.length();
              if (LinePos+n>Line.length()) return false;
              for (int i=0; i<n; i++)
                     if (s.charAt(i)!=Line.charAt(LinePos+i)) return false;
              return true;
       }

Here is the caller graph for this function:

void rene.util.xml.XmlReader.init ( InputStream  in) throws XmlReaderException [inline]

Definition at line 102 of file XmlReader.java.

       {      try
              {      // read the file into a buffer
                     BufferedInputStream rin=new BufferedInputStream(in);
                     SimpleByteBuffer bb=new SimpleByteBuffer(10000);
                     while (true)
                     {      int k=rin.read();
                            if (k<0) break;
                            bb.append((byte)k);
                     }
                     rin.close();
                     byte b[]=bb.getByteArray();
                     
                     // Try to open an ASCII stream, or a default stream
                     ByteArrayInputStream bin=new ByteArrayInputStream(b);
                     XmlReader R=null;
                     try
                     {      R=new XmlReader(new BufferedReader(new InputStreamReader(bin,"ASCII")));
                     }
                     catch (UnsupportedEncodingException ex)
                     {      R=new XmlReader(new BufferedReader(new InputStreamReader(bin)));
                     }             
                                          
                     // Determine the encoding
                     String Encoding=null;
                     while (true)
                     {      while (true)
                            {      int c=R.read();
                                   if (c==-1) throw new Exception("<?xml> tag not found");
                                   if (c=='<') break;
                            }
                            if (R.found("?xml"))
                            {      String s=R.scanFor("?>");
                                   if (s==null) throw new Exception("<?xml> tag error");
                                   int n=s.indexOf("encoding=\"");
                                   if (n>=0)
                                   {      n+="encoding=\"".length();
                                          s=s.substring(n);
                                          int m=s.indexOf('\"');
                                          if (m<0) throw new Exception("Closing bracket missing");
                                          Encoding=s.substring(0,m).toUpperCase();
                                          if (Encoding.equals("UTF-8")) Encoding="UTF8";
                                                 // for IE5 !
                                          break;
                                   }
                                   break;
                            }
                     }
                     
                     // Open a stream with this encoding
                     bin=new ByteArrayInputStream(b);
                     if (Encoding==null)
                            In=new BufferedReader(new InputStreamReader(bin));
                     else
                            try 
                            {      In=new BufferedReader(new InputStreamReader(
                                          bin,Encoding));
                            }
                            catch (UnsupportedEncodingException e)
                            {      try
                                   {      In=new BufferedReader(new InputStreamReader(
                                                 bin,"ASCII"));
                                   }
                                   catch (UnsupportedEncodingException ex)
                                   {      In=new BufferedReader(new InputStreamReader(bin));
                                   }                                  
                            }
              }
              catch (Exception e)
              {      throw new XmlReaderException(e.toString());
              }
       }

Here is the call graph for this function:

static void rene.util.xml.XmlReader.main ( String  args[]) [inline, static]

A test program.

Definition at line 415 of file XmlReader.java.

       {      try
              {      BufferedReader in=new BufferedReader(
                            new InputStreamReader(
                            new FileInputStream("rene\\util\\xml\\test.xml"),"UTF8"));
                     XmlReader reader=new XmlReader(in);
                     XmlTree tree=reader.scan();
                     in.close();
                     print(tree);
              }
              catch (XmlReaderException e)
              {      System.out.println(e.toString()+"\n"+
                            e.getLine()+"\n"+"Position : "+e.getPos());
              }
              catch (IOException e)
              {      System.out.println(e);
              }
       }

Here is the call graph for this function:

static void rene.util.xml.XmlReader.print ( XmlTree  t) [inline, static]

Definition at line 434 of file XmlReader.java.

       {      XmlTag tag=t.getTag();
              System.out.print("<"+tag.name());
              for (int i=0; i<tag.countParams(); i++)
              {      System.out.print(" "+tag.getParam(i)+"=\""+tag.getValue(i)+"\"");
              }
              System.out.println(">");
              ListElement el=t.children().first();
              while (el!=null)
              {      print((XmlTree)(el.content()));
                     el=el.next();
              }
              System.out.println("</"+tag.name()+">");
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 376 of file XmlReader.java.

       {      try
              {      if (Line==null)
                     {      Line=In.readLine();
                            LinePos=0;
                            // System.out.println("Read --> "+Line);
                     }
                     if (Line==null) return -1;
                     if (LinePos>=Line.length())
                     {      Line=null;
                            return '\n';
                     }
                     return Line.charAt(LinePos++);
              }
              catch (Exception e)
              {      return -1;
              }
       }

Here is the caller graph for this function:

Scan an xml file.

This function reads, until <?xml is found. then it skips this declaration and scans the rest of the items.

Definition at line 181 of file XmlReader.java.

       {      while (true)
              {      while (true)
                     {      int c=read();
                            if (c==-1) return null;
                            if (c=='<') break;
                     }
                     if (found("?xml"))
                     {      String s=scanFor("?>");
                            if (s==null) return null;
                            XmlTree t=new XmlTree(new XmlTagRoot());
                            t.addchild(new XmlTree(new XmlTagPI(s)));
                            scanContent(t);
                            return t;
                     }
              }
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 200 of file XmlReader.java.

       {      //System.out.println("Sanning for "+t.getTag().name()+" ("+
              //     t.getTag().countParams()+")");
              while (true)
              {      String s=scanFor('<');
                     if (s==null)
                     {      if (t.getTag() instanceof XmlTagRoot) return;
                            exception("File ended surprisingly");
                     }
                     if (!empty(s))
                     {      t.addchild(new XmlTree(new XmlTagText(
                                   XmlTranslator.toText(s))));
                     }
                     if (found("!--"))
                     {      s=scanFor("-->");
                            continue;
                     }
                     if (found("!"))
                     {      s=scanTagFor('>');
                            continue;
                     }
                     if (found("?"))
                     {      s=scanTagFor("?>");
                            t.addchild(new XmlTree(new XmlTagPI(s)));
                            continue;
                     }
                     s=scanTagFor('>');
                     if (s==null)
                            exception("> missing");
                     if (s.startsWith("/"))
                     {      if (s.substring(1).equals(t.getTag().name()))
                                   return;
                            else 
                                   exception("End tag without start tag");
                     }
                     if (s.endsWith("/"))
                     {      t.addchild(new XmlTree(new XmlTag(s.substring(0,s.length()-1))));
                     }
                     else
                     {      XmlTree t0=new XmlTree(new XmlTag(s));
                            scanContent(t0);
                            t.addchild(t0);
                     }
              }
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Scan for an end character.

Returns:
String between the current position and the end character, or null.

Definition at line 273 of file XmlReader.java.

       {      buf.clear();
              int c=read();
              while (c!=end)
              {      buf.append((char)c);
                     c=read();
                     if (c<0) return null;
              }
              return buf.toString();
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Scan for a specific string.

Returns:
String between the current position and the string.

Definition at line 289 of file XmlReader.java.

       {      buf.clear();
              while (!found(s))
              {      int c=read();
                     if (c<0) return null;
                     buf.append((char)c);
              }
              for (int i=0; i<s.length(); i++) read();
              return buf.toString();
       }

Here is the call graph for this function:

Scan tag for an end character (interpreting " and ')

Returns:
String between the current position and the end character, or null.

Definition at line 305 of file XmlReader.java.

       {      buf.clear();
              int c=read();
              while (c!=end)
              {      if (c=='\"')
                     {      buf.append((char)c);
                            while (true)
                            {      c=read();
                                   if (c<0) return null;
                                   if (c=='\"') break;
                                   buf.append((char)c);
                            }
                            buf.append((char)c);
                     }
                     else if (c=='\'')
                     {      buf.append((char)c);
                            while (true)
                            {      c=read();
                                   if (c<0) return null;
                                   if (c=='\'') break;
                                   buf.append((char)c);
                            }
                            buf.append((char)c);
                     }
                     else buf.append((char)c);
                     c=read();
                     if (c<0) return null;
              }
              return buf.toString();
       }

Here is the call graph for this function:

Here is the caller graph for this function:

Scan tag for a specific string (interpreting " and ')

Returns:
String between the current position and the string.

Definition at line 341 of file XmlReader.java.

       {      buf.clear();
              while (!found(s))
              {      int c=read();
                     if (c<0) return null;
                     if (c=='\"')
                     {      buf.append((char)c);
                            while (true)
                            {      c=read();
                                   if (c<0) return null;
                                   if (c=='\"') break;
                                   buf.append((char)c);
                            }
                            buf.append((char)c);
                     }
                     else if (c=='\'')
                     {      buf.append((char)c);
                            while (true)
                            {      c=read();
                                   if (c<0) return null;
                                   if (c=='\'') break;
                                   buf.append((char)c);
                            }
                            buf.append((char)c);
                     }
                     else buf.append((char)c);
              }
              for (int i=0; i<s.length(); i++) read();
              return buf.toString();
       }

Here is the call graph for this function:

Skip Blanks.

Returns:
Non-blank character or -1 for EOF.

Definition at line 260 of file XmlReader.java.

       {      while (true)
              {      int c=read();
                     if (c==' ' || c=='\t' || c=='\n') continue;
                     else return c;
              }
       }

Here is the call graph for this function:

static boolean rene.util.xml.XmlReader.testXml ( String  s) [inline, static]

Definition at line 449 of file XmlReader.java.

       {      int i=0;
              while (i<s.length())
              {      char c=s.charAt(i);
                     if (c=='<') break;
                     i++;
              }
              if (i>=s.length()) return false;
              if (s.substring(i).startsWith("<?xml") || s.substring(i).startsWith("< ?xml") ) return true;
              return false;
       }

Member Data Documentation

Definition at line 18 of file XmlReader.java.

BufferedReader rene.util.xml.XmlReader.In [package]

Definition at line 17 of file XmlReader.java.

Definition at line 373 of file XmlReader.java.

Definition at line 374 of file XmlReader.java.


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