langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/LayoutParser.java
changeset 5855 00d9c252e60c
parent 5520 86e4b9a9da40
child 7614 cfadc977ca75
equal deleted inserted replaced
5854:5dc61238219e 5855:00d9c252e60c
    43 public class LayoutParser extends DefaultHandler {
    43 public class LayoutParser extends DefaultHandler {
    44 
    44 
    45     /**
    45     /**
    46      * The map of XML elements that have been parsed.
    46      * The map of XML elements that have been parsed.
    47      */
    47      */
    48     private Map<String,List<Object>> xmlElementsMap;
    48     private Map<String,XMLNode> xmlElementsMap;
    49 
    49     private XMLNode currentNode;
    50     private Configuration configuration;
    50     private Configuration configuration;
    51     private static LayoutParser instance;
    51     private static LayoutParser instance;
    52     private String currentRoot;
    52     private String currentRoot;
    53     private boolean isParsing;
    53     private boolean isParsing;
    54 
    54 
    55     /**
    55     /**
    56      * This class is a singleton.
    56      * This class is a singleton.
    57      */
    57      */
    58     private LayoutParser(Configuration configuration) {
    58     private LayoutParser(Configuration configuration) {
    59         xmlElementsMap = new HashMap<String,List<Object>>();
    59         xmlElementsMap = new HashMap<String,XMLNode>();
    60         this.configuration = configuration;
    60         this.configuration = configuration;
    61     }
    61     }
    62 
    62 
    63     /**
    63     /**
    64      * Return an instance of the BuilderXML.
    64      * Return an instance of the BuilderXML.
    76     /**
    76     /**
    77      * Parse the XML specifying the layout of the documentation.
    77      * Parse the XML specifying the layout of the documentation.
    78      *
    78      *
    79      * @return List the list of XML elements parsed.
    79      * @return List the list of XML elements parsed.
    80      */
    80      */
    81     public List<?> parseXML(String root) {
    81     public XMLNode parseXML(String root) {
    82         if (xmlElementsMap.containsKey(root)) {
    82         if (xmlElementsMap.containsKey(root)) {
    83             return xmlElementsMap.get(root);
    83             return xmlElementsMap.get(root);
    84         }
    84         }
    85         try {
    85         try {
    86             List<Object> xmlElements = new ArrayList<Object>();
       
    87             xmlElementsMap.put(root, xmlElements);
       
    88             currentRoot = root;
    86             currentRoot = root;
    89             isParsing = false;
    87             isParsing = false;
    90             SAXParserFactory factory = SAXParserFactory.newInstance();
    88             SAXParserFactory factory = SAXParserFactory.newInstance();
    91             SAXParser saxParser = factory.newSAXParser();
    89             SAXParser saxParser = factory.newSAXParser();
    92             InputStream in = configuration.getBuilderXML();
    90             InputStream in = configuration.getBuilderXML();
    93             saxParser.parse(in, this);
    91             saxParser.parse(in, this);
    94             return xmlElements;
    92             return xmlElementsMap.get(root);
    95         } catch (Throwable t) {
    93         } catch (Throwable t) {
    96             t.printStackTrace();
    94             t.printStackTrace();
    97             throw new DocletAbortException();
    95             throw new DocletAbortException();
    98         }
    96         }
    99     }
    97     }
   100 
    98 
   101     /**
    99     /**
   102      * {@inheritDoc}
   100      * {@inheritDoc}
   103      */
   101      */
       
   102     @Override
   104     public void startElement(String namespaceURI, String sName, String qName,
   103     public void startElement(String namespaceURI, String sName, String qName,
   105         Attributes attrs)
   104         Attributes attrs)
   106     throws SAXException {
   105     throws SAXException {
   107         if (isParsing || qName.equals(currentRoot)) {
   106         if (isParsing || qName.equals(currentRoot)) {
   108             isParsing = true;
   107             isParsing = true;
   109             List<Object> xmlElements = xmlElementsMap.get(currentRoot);
   108             currentNode = new XMLNode(currentNode, qName);
   110             xmlElements.add(qName);
   109             for (int i = 0; i < attrs.getLength(); i++)
       
   110                 currentNode.attrs.put(attrs.getLocalName(i), attrs.getValue(i));
       
   111             if (qName.equals(currentRoot))
       
   112                 xmlElementsMap.put(qName, currentNode);
   111         }
   113         }
   112     }
   114     }
   113 
   115 
   114     /**
   116     /**
   115      * {@inheritDoc}
   117      * {@inheritDoc}
   116      */
   118      */
       
   119     @Override
   117     public void endElement(String namespaceURI, String sName, String qName)
   120     public void endElement(String namespaceURI, String sName, String qName)
   118     throws SAXException {
   121     throws SAXException {
   119         if (! isParsing) {
   122         if (! isParsing) {
   120             isParsing = false;
       
   121             return;
   123             return;
   122         }
   124         }
   123         List<Object> xmlElements = xmlElementsMap.get(currentRoot);
   125         currentNode = currentNode.parent;
   124         if (xmlElements.get(xmlElements.size()-1).equals(qName)) {
       
   125             return;
       
   126         } else {
       
   127             List<Object> subElements = new ArrayList<Object>();
       
   128             int targetIndex = xmlElements.indexOf(qName);
       
   129             int size = xmlElements.size();
       
   130             for (int i = targetIndex; i < size; i++) {
       
   131                 subElements.add(xmlElements.get(targetIndex));
       
   132                 xmlElements.remove(targetIndex);
       
   133             }
       
   134             //Save the sub elements as a list.
       
   135             xmlElements.add(subElements);
       
   136         }
       
   137         isParsing = ! qName.equals(currentRoot);
   126         isParsing = ! qName.equals(currentRoot);
   138     }
   127     }
   139 }
   128 }