View Javadoc

1   /*
2    * $Id: XMLPrologParser.java,v 1.3 2005/03/29 15:07:42 jlerner Exp $
3    *
4    * Copyright (c) 1999-2004, BBN Technologies, LLC.
5    * All rights reserved.
6    * http://www.daml.org/legal/opensource/bbn_license.html
7    */
8    
9   package com.bbn.swede.core.dom;
10  
11  import java.util.ArrayList;
12  
13  /***
14   * A parser to create nodes for a document's XML prolog.  This includes the XML
15   * version line, DOCTYPE, and any additional processing instructions.  The SAX
16   * parser that comes with Java handles these internally and only provides
17   * notifications to the user-defined handler for tags and attributes.  This
18   * preprocessor allows prolog elements to be treated as part of the document's
19   * abstract syntax tree.
20   * @author jlerner
21   */
22  public class XMLPrologParser
23  {
24  
25     /***
26      * Creates an XMLPrologParser.
27      */
28     public XMLPrologParser()
29     {
30        super();
31     }
32  
33     private static final String S_DOCTYPE = "!DOCTYPE";
34     /***
35      * Parses the prolog of an XML document into an array of XML processing 
36      * instruction and DOCTYPE nodes.
37      * @param sProlog A string containing either a partial or full XML prolog.
38      *                XML comments should be blanked out of the string before
39      *                it is parsed.
40      * @return An array of OASTNodes retrieved from the supplied string.  The
41      *         nodes will be of type ProcessingInstruction or Doctype.
42      */
43     public OASTNode[] parse(String sProlog)
44     {
45        ArrayList al = new ArrayList();
46        int iPos = sProlog.indexOf('<');
47        int iEnd;
48        while (iPos >= 0)
49        {
50           char ch = sProlog.charAt(iPos + 1);
51           if (ch == '?')
52           {
53              iEnd = sProlog.indexOf("?>", iPos);
54              iEnd += 2;
55              ProcessingInstruction pi;
56              String sText = sProlog.substring(iPos, iEnd);
57              if (sText.startsWith("<?xml"))
58              {
59                 pi = new XMLVersion(sText);
60              }
61              else
62              {
63                 pi = new ProcessingInstruction(sText);
64              }
65              pi.setOffset(iPos);
66              pi.setLength(iEnd - iPos);
67              iPos = sProlog.indexOf('<', iEnd + 1);
68              al.add(pi);
69              continue;
70           }
71  
72           int iOffset = iEnd = iPos;
73           do
74           {
75              iEnd = sProlog.indexOf('>', iEnd + 1);
76              iPos = sProlog.indexOf('<', iPos + 1);
77           } while (iPos >= 0 && iPos < iEnd);
78           iEnd += 1;
79           Doctype d = new Doctype(sProlog.substring(iOffset, iEnd));
80           d.setOffset(iOffset);
81           d.setLength(iEnd - iOffset);
82           al.add(d);
83        }
84        
85        return (OASTNode[])al.toArray(new OASTNode[0]);
86     }
87  }