langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/LayoutParser.java
author briangoetz
Wed, 18 Dec 2013 16:05:18 -0500
changeset 22163 3651128c74eb
parent 19667 fdfce85627a9
permissions -rw-r--r--
8030244: Update langtools to use Diamond Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
19667
fdfce85627a9 8001669: javadoc internal DocletAbortException should set cause when appropriate
jjg
parents: 14542
diff changeset
     2
 * Copyright (c) 2003, 2013, 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
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
package com.sun.tools.doclets.internal.toolkit.builders;
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import java.util.*;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 7681
diff changeset
    29
8d2148961366 8000663: clean up langtools imports
jjg
parents: 7681
diff changeset
    30
import javax.xml.parsers.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 7681
diff changeset
    31
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import org.xml.sax.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import org.xml.sax.helpers.DefaultHandler;
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 7681
diff changeset
    34
8d2148961366 8000663: clean up langtools imports
jjg
parents: 7681
diff changeset
    35
import com.sun.tools.doclets.internal.toolkit.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 7681
diff changeset
    36
import com.sun.tools.doclets.internal.toolkit.util.*;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * Parse the XML that specified the order of operation for the builders.  This
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * Parser uses SAX parsing.
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    42
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    43
 *  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
    44
 *  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
    45
 *  deletion without notice.</b>
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    46
 *
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * @author Jamie Ho
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * @since 1.5
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * @see SAXParser
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
public class LayoutParser extends DefaultHandler {
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     * The map of XML elements that have been parsed.
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     */
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
    56
    private Map<String,XMLNode> xmlElementsMap;
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
    57
    private XMLNode currentNode;
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14260
diff changeset
    58
    private final Configuration configuration;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
    private String currentRoot;
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    private boolean isParsing;
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    private LayoutParser(Configuration configuration) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19667
diff changeset
    63
        xmlElementsMap = new HashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        this.configuration = configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     * Return an instance of the BuilderXML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * @param configuration the current configuration of the doclet.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * @return an instance of the BuilderXML.
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
    public static LayoutParser getInstance(Configuration configuration) {
14542
7062120649c2 8000800: javadoc uses static non-final fields
jjg
parents: 14260
diff changeset
    74
        return new LayoutParser(configuration);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * Parse the XML specifying the layout of the documentation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     *
7614
cfadc977ca75 6851834: Javadoc doclet needs a structured approach to generate the output HTML.
bpatel
parents: 5855
diff changeset
    80
     * @return the list of XML elements parsed.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
     */
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
    82
    public XMLNode parseXML(String root) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
        if (xmlElementsMap.containsKey(root)) {
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1264
diff changeset
    84
            return xmlElementsMap.get(root);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            currentRoot = root;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
            isParsing = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
            SAXParserFactory factory = SAXParserFactory.newInstance();
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
            SAXParser saxParser = factory.newSAXParser();
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
            InputStream in = configuration.getBuilderXML();
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
            saxParser.parse(in, this);
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
    93
            return xmlElementsMap.get(root);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
        } catch (Throwable t) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            t.printStackTrace();
19667
fdfce85627a9 8001669: javadoc internal DocletAbortException should set cause when appropriate
jjg
parents: 14542
diff changeset
    96
            throw new DocletAbortException(t);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
     */
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   103
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
    public void startElement(String namespaceURI, String sName, String qName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        Attributes attrs)
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
    throws SAXException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
        if (isParsing || qName.equals(currentRoot)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            isParsing = true;
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   109
            currentNode = new XMLNode(currentNode, qName);
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   110
            for (int i = 0; i < attrs.getLength(); i++)
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   111
                currentNode.attrs.put(attrs.getLocalName(i), attrs.getValue(i));
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   112
            if (qName.equals(currentRoot))
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   113
                xmlElementsMap.put(qName, currentNode);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     * {@inheritDoc}
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     */
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   120
    @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    public void endElement(String namespaceURI, String sName, String qName)
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
    throws SAXException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        if (! isParsing) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
            return;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        }
5855
00d9c252e60c 6961178: Allow doclet.xml to contain XML attributes
jjg
parents: 5520
diff changeset
   126
        currentNode = currentNode.parent;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
        isParsing = ! qName.equals(currentRoot);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
}