src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/LayoutParser.java

changeset 0
959103a6100f
child 2525
2eb010b6cb22
equal deleted inserted replaced
-1:000000000000 0:959103a6100f
1 /*
2 * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25 package com.sun.tools.doclets.internal.toolkit.builders;
26
27 import java.io.*;
28 import java.util.*;
29
30 import javax.xml.parsers.*;
31
32 import org.xml.sax.*;
33 import org.xml.sax.helpers.DefaultHandler;
34
35 import com.sun.tools.doclets.internal.toolkit.*;
36 import com.sun.tools.doclets.internal.toolkit.util.*;
37
38 /**
39 * Parse the XML that specified the order of operation for the builders. This
40 * Parser uses SAX parsing.
41 *
42 * <p><b>This is NOT part of any supported API.
43 * If you write code that depends on this, you do so at your own risk.
44 * This code and its internal interfaces are subject to change or
45 * deletion without notice.</b>
46 *
47 * @author Jamie Ho
48 * @since 1.5
49 * @see SAXParser
50 */
51 public class LayoutParser extends DefaultHandler {
52
53 /**
54 * The map of XML elements that have been parsed.
55 */
56 private Map<String,XMLNode> xmlElementsMap;
57 private XMLNode currentNode;
58 private final Configuration configuration;
59 private String currentRoot;
60 private boolean isParsing;
61
62 private LayoutParser(Configuration configuration) {
63 xmlElementsMap = new HashMap<String,XMLNode>();
64 this.configuration = configuration;
65 }
66
67 /**
68 * Return an instance of the BuilderXML.
69 *
70 * @param configuration the current configuration of the doclet.
71 * @return an instance of the BuilderXML.
72 */
73 public static LayoutParser getInstance(Configuration configuration) {
74 return new LayoutParser(configuration);
75 }
76
77 /**
78 * Parse the XML specifying the layout of the documentation.
79 *
80 * @return the list of XML elements parsed.
81 */
82 public XMLNode parseXML(String root) {
83 if (xmlElementsMap.containsKey(root)) {
84 return xmlElementsMap.get(root);
85 }
86 try {
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 }
99
100 /**
101 * {@inheritDoc}
102 */
103 @Override
104 public void startElement(String namespaceURI, String sName, String qName,
105 Attributes attrs)
106 throws SAXException {
107 if (isParsing || qName.equals(currentRoot)) {
108 isParsing = true;
109 currentNode = new XMLNode(currentNode, qName);
110 for (int i = 0; i < attrs.getLength(); i++)
111 currentNode.attrs.put(attrs.getLocalName(i), attrs.getValue(i));
112 if (qName.equals(currentRoot))
113 xmlElementsMap.put(qName, currentNode);
114 }
115 }
116
117 /**
118 * {@inheritDoc}
119 */
120 @Override
121 public void endElement(String namespaceURI, String sName, String qName)
122 throws SAXException {
123 if (! isParsing) {
124 return;
125 }
126 currentNode = currentNode.parent;
127 isParsing = ! qName.equals(currentRoot);
128 }
129 }

mercurial