langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/builders/LayoutParser.java
author jjg
Mon, 22 Aug 2016 16:32:40 -0700
changeset 40587 1c355ea550ed
parent 35426 374342e56a56
child 45417 f7479ee8de69
permissions -rw-r--r--
8164130: Simplify doclet IOException handling Reviewed-by: bpatel, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 34752
diff changeset
     2
 * Copyright (c) 2003, 2016, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
34752
9c262a013456 8145342: Some copyright notices are inconsistently and ill formatted
vasya
parents: 25874
diff changeset
    25
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 34752
diff changeset
    26
package jdk.javadoc.internal.doclets.toolkit.builders;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
import java.util.*;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 7681
diff changeset
    30
8d2148961366 8000663: clean up langtools imports
jjg
parents: 7681
diff changeset
    31
import javax.xml.parsers.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 7681
diff changeset
    32
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 34752
diff changeset
    33
import jdk.javadoc.internal.doclets.toolkit.Configuration;
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    34
import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException;
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    35
import jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
import org.xml.sax.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
import org.xml.sax.helpers.DefaultHandler;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 7681
diff changeset
    38
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * Parse the XML that specified the order of operation for the builders.  This
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * Parser uses SAX parsing.
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    44
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    45
 *  If you write code that depends on this, you do so at your own risk.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    46
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    47
 *  deletion without notice.</b>
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    48
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * @author Jamie Ho
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * @see SAXParser
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
public class LayoutParser extends DefaultHandler {
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     * The map of XML elements that have been parsed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    57
    private final Map<String,XMLNode> xmlElementsMap;
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
    58
    private XMLNode currentNode;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14260
diff changeset
    59
    private final Configuration configuration;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private String currentRoot;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    private boolean isParsing;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    private LayoutParser(Configuration configuration) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19667
diff changeset
    64
        xmlElementsMap = new HashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
        this.configuration = configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     * Return an instance of the BuilderXML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * @param configuration the current configuration of the doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * @return an instance of the BuilderXML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    public static LayoutParser getInstance(Configuration configuration) {
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14260
diff changeset
    75
        return new LayoutParser(configuration);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     * Parse the XML specifying the layout of the documentation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
     *
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    81
     * @param root the name of the desired node
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5855
diff changeset
    82
     * @return the list of XML elements parsed.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    83
     * @throws DocFileIOException if there is a problem reading a user-supplied build file
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    84
     * @throws SimpleDocletException if there is a problem reading the system build file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
     */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    86
    public XMLNode parseXML(String root) throws DocFileIOException, SimpleDocletException {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    87
        if (!xmlElementsMap.containsKey(root)) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    88
            try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    89
                currentRoot = root;
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    90
                isParsing = false;
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    91
                SAXParserFactory factory = SAXParserFactory.newInstance();
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    92
                SAXParser saxParser = factory.newSAXParser();
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    93
                InputStream in = configuration.getBuilderXML();
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    94
                saxParser.parse(in, this);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    95
            } catch (IOException | ParserConfigurationException | SAXException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    96
                String message = (configuration.builderXMLPath == null)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    97
                        ? configuration.getResources().getText("doclet.exception.read.resource",
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    98
                                Configuration.DEFAULT_BUILDER_XML, e)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
    99
                        : configuration.getResources().getText("doclet.exception.read.file",
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   100
                                configuration.builderXMLPath, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   101
                throw new SimpleDocletException(message, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   102
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
        }
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   104
        return xmlElementsMap.get(root);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     */
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   110
    @Override
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   111
    public void startElement(String namespaceURI, String sName, String qName, Attributes attrs)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 35426
diff changeset
   112
            throws SAXException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        if (isParsing || qName.equals(currentRoot)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            isParsing = true;
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   115
            currentNode = new XMLNode(currentNode, qName);
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   116
            for (int i = 0; i < attrs.getLength(); i++)
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   117
                currentNode.attrs.put(attrs.getLocalName(i), attrs.getValue(i));
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   118
            if (qName.equals(currentRoot))
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   119
                xmlElementsMap.put(qName, currentNode);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     */
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   126
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    public void endElement(String namespaceURI, String sName, String qName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    throws SAXException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        if (! isParsing) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        }
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   132
        currentNode = currentNode.parent;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
        isParsing = ! qName.equals(currentRoot);
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
}