langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/builders/LayoutParser.java
changeset 40587 1c355ea550ed
parent 35426 374342e56a56
child 45417 f7479ee8de69
equal deleted inserted replaced
40519:e17429a7e843 40587:1c355ea550ed
    29 import java.util.*;
    29 import java.util.*;
    30 
    30 
    31 import javax.xml.parsers.*;
    31 import javax.xml.parsers.*;
    32 
    32 
    33 import jdk.javadoc.internal.doclets.toolkit.Configuration;
    33 import jdk.javadoc.internal.doclets.toolkit.Configuration;
    34 import jdk.javadoc.internal.doclets.toolkit.util.DocletAbortException;
    34 import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
       
    35 import jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException;
    35 import org.xml.sax.*;
    36 import org.xml.sax.*;
    36 import org.xml.sax.helpers.DefaultHandler;
    37 import org.xml.sax.helpers.DefaultHandler;
    37 
    38 
    38 
    39 
    39 /**
    40 /**
    51 public class LayoutParser extends DefaultHandler {
    52 public class LayoutParser extends DefaultHandler {
    52 
    53 
    53     /**
    54     /**
    54      * The map of XML elements that have been parsed.
    55      * The map of XML elements that have been parsed.
    55      */
    56      */
    56     private Map<String,XMLNode> xmlElementsMap;
    57     private final Map<String,XMLNode> xmlElementsMap;
    57     private XMLNode currentNode;
    58     private XMLNode currentNode;
    58     private final Configuration configuration;
    59     private final Configuration configuration;
    59     private String currentRoot;
    60     private String currentRoot;
    60     private boolean isParsing;
    61     private boolean isParsing;
    61 
    62 
    75     }
    76     }
    76 
    77 
    77     /**
    78     /**
    78      * Parse the XML specifying the layout of the documentation.
    79      * Parse the XML specifying the layout of the documentation.
    79      *
    80      *
       
    81      * @param root the name of the desired node
    80      * @return the list of XML elements parsed.
    82      * @return the list of XML elements parsed.
       
    83      * @throws DocFileIOException if there is a problem reading a user-supplied build file
       
    84      * @throws SimpleDocletException if there is a problem reading the system build file
    81      */
    85      */
    82     public XMLNode parseXML(String root) {
    86     public XMLNode parseXML(String root) throws DocFileIOException, SimpleDocletException {
    83         if (xmlElementsMap.containsKey(root)) {
    87         if (!xmlElementsMap.containsKey(root)) {
    84             return xmlElementsMap.get(root);
    88             try {
       
    89                 currentRoot = root;
       
    90                 isParsing = false;
       
    91                 SAXParserFactory factory = SAXParserFactory.newInstance();
       
    92                 SAXParser saxParser = factory.newSAXParser();
       
    93                 InputStream in = configuration.getBuilderXML();
       
    94                 saxParser.parse(in, this);
       
    95             } catch (IOException | ParserConfigurationException | SAXException e) {
       
    96                 String message = (configuration.builderXMLPath == null)
       
    97                         ? configuration.getResources().getText("doclet.exception.read.resource",
       
    98                                 Configuration.DEFAULT_BUILDER_XML, e)
       
    99                         : configuration.getResources().getText("doclet.exception.read.file",
       
   100                                 configuration.builderXMLPath, e);
       
   101                 throw new SimpleDocletException(message, e);
       
   102             }
    85         }
   103         }
    86         try {
   104         return xmlElementsMap.get(root);
    87             currentRoot = root;
       
    88             isParsing = false;
       
    89             SAXParserFactory factory = SAXParserFactory.newInstance();
       
    90             SAXParser saxParser = factory.newSAXParser();
       
    91             InputStream in = configuration.getBuilderXML();
       
    92             saxParser.parse(in, this);
       
    93             return xmlElementsMap.get(root);
       
    94         } catch (Throwable t) {
       
    95             t.printStackTrace();
       
    96             throw new DocletAbortException(t);
       
    97         }
       
    98     }
   105     }
    99 
   106 
   100     /**
   107     /**
   101      * {@inheritDoc}
   108      * {@inheritDoc}
   102      */
   109      */
   103     @Override
   110     @Override
   104     public void startElement(String namespaceURI, String sName, String qName,
   111     public void startElement(String namespaceURI, String sName, String qName, Attributes attrs)
   105         Attributes attrs)
   112             throws SAXException {
   106     throws SAXException {
       
   107         if (isParsing || qName.equals(currentRoot)) {
   113         if (isParsing || qName.equals(currentRoot)) {
   108             isParsing = true;
   114             isParsing = true;
   109             currentNode = new XMLNode(currentNode, qName);
   115             currentNode = new XMLNode(currentNode, qName);
   110             for (int i = 0; i < attrs.getLength(); i++)
   116             for (int i = 0; i < attrs.getLength(); i++)
   111                 currentNode.attrs.put(attrs.getLocalName(i), attrs.getValue(i));
   117                 currentNode.attrs.put(attrs.getLocalName(i), attrs.getValue(i));