author | ksrini |
Mon, 05 Jun 2017 13:45:34 -0700 | |
changeset 45417 | f7479ee8de69 |
parent 40587 | 1c355ea550ed |
permissions | -rw-r--r-- |
10 | 1 |
/* |
45417 | 2 |
* Copyright (c) 2003, 2017, Oracle and/or its affiliates. All rights reserved. |
10 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5520 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
10 | 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 | 27 |
|
28 |
import java.io.*; |
|
29 |
import java.util.*; |
|
14258 | 30 |
|
31 |
import javax.xml.parsers.*; |
|
32 |
||
45417 | 33 |
import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration; |
40587 | 34 |
import jdk.javadoc.internal.doclets.toolkit.util.DocFileIOException; |
35 |
import jdk.javadoc.internal.doclets.toolkit.util.SimpleDocletException; |
|
10 | 36 |
import org.xml.sax.*; |
37 |
import org.xml.sax.helpers.DefaultHandler; |
|
14258 | 38 |
|
10 | 39 |
|
40 |
/** |
|
41 |
* Parse the XML that specified the order of operation for the builders. This |
|
42 |
* Parser uses SAX parsing. |
|
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 | 49 |
* @author Jamie Ho |
50 |
* @see SAXParser |
|
51 |
*/ |
|
52 |
public class LayoutParser extends DefaultHandler { |
|
53 |
||
54 |
/** |
|
55 |
* The map of XML elements that have been parsed. |
|
56 |
*/ |
|
40587 | 57 |
private final Map<String,XMLNode> xmlElementsMap; |
5855 | 58 |
private XMLNode currentNode; |
45417 | 59 |
private final BaseConfiguration configuration; |
10 | 60 |
private String currentRoot; |
61 |
private boolean isParsing; |
|
62 |
||
45417 | 63 |
private LayoutParser(BaseConfiguration configuration) { |
22163 | 64 |
xmlElementsMap = new HashMap<>(); |
10 | 65 |
this.configuration = configuration; |
66 |
} |
|
67 |
||
68 |
/** |
|
69 |
* Return an instance of the BuilderXML. |
|
70 |
* |
|
71 |
* @param configuration the current configuration of the doclet. |
|
72 |
* @return an instance of the BuilderXML. |
|
73 |
*/ |
|
45417 | 74 |
public static LayoutParser getInstance(BaseConfiguration configuration) { |
14542 | 75 |
return new LayoutParser(configuration); |
10 | 76 |
} |
77 |
||
78 |
/** |
|
79 |
* Parse the XML specifying the layout of the documentation. |
|
80 |
* |
|
40587 | 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 | 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 |
|
10 | 85 |
*/ |
40587 | 86 |
public XMLNode parseXML(String root) throws DocFileIOException, SimpleDocletException { |
87 |
if (!xmlElementsMap.containsKey(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", |
|
45417 | 98 |
BaseConfiguration.DEFAULT_BUILDER_XML, e) |
40587 | 99 |
: configuration.getResources().getText("doclet.exception.read.file", |
100 |
configuration.builderXMLPath, e); |
|
101 |
throw new SimpleDocletException(message, e); |
|
102 |
} |
|
10 | 103 |
} |
40587 | 104 |
return xmlElementsMap.get(root); |
10 | 105 |
} |
106 |
||
107 |
/** |
|
108 |
* {@inheritDoc} |
|
109 |
*/ |
|
5855 | 110 |
@Override |
40587 | 111 |
public void startElement(String namespaceURI, String sName, String qName, Attributes attrs) |
112 |
throws SAXException { |
|
10 | 113 |
if (isParsing || qName.equals(currentRoot)) { |
114 |
isParsing = true; |
|
5855 | 115 |
currentNode = new XMLNode(currentNode, qName); |
116 |
for (int i = 0; i < attrs.getLength(); i++) |
|
117 |
currentNode.attrs.put(attrs.getLocalName(i), attrs.getValue(i)); |
|
118 |
if (qName.equals(currentRoot)) |
|
119 |
xmlElementsMap.put(qName, currentNode); |
|
10 | 120 |
} |
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* {@inheritDoc} |
|
125 |
*/ |
|
5855 | 126 |
@Override |
10 | 127 |
public void endElement(String namespaceURI, String sName, String qName) |
128 |
throws SAXException { |
|
129 |
if (! isParsing) { |
|
130 |
return; |
|
131 |
} |
|
5855 | 132 |
currentNode = currentNode.parent; |
10 | 133 |
isParsing = ! qName.equals(currentRoot); |
134 |
} |
|
135 |
} |