1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/doclets/internal/toolkit/builders/LayoutParser.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,129 @@ 1.4 +/* 1.5 + * Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 +package com.sun.tools.doclets.internal.toolkit.builders; 1.29 + 1.30 +import java.io.*; 1.31 +import java.util.*; 1.32 + 1.33 +import javax.xml.parsers.*; 1.34 + 1.35 +import org.xml.sax.*; 1.36 +import org.xml.sax.helpers.DefaultHandler; 1.37 + 1.38 +import com.sun.tools.doclets.internal.toolkit.*; 1.39 +import com.sun.tools.doclets.internal.toolkit.util.*; 1.40 + 1.41 +/** 1.42 + * Parse the XML that specified the order of operation for the builders. This 1.43 + * Parser uses SAX parsing. 1.44 + * 1.45 + * <p><b>This is NOT part of any supported API. 1.46 + * If you write code that depends on this, you do so at your own risk. 1.47 + * This code and its internal interfaces are subject to change or 1.48 + * deletion without notice.</b> 1.49 + * 1.50 + * @author Jamie Ho 1.51 + * @since 1.5 1.52 + * @see SAXParser 1.53 + */ 1.54 +public class LayoutParser extends DefaultHandler { 1.55 + 1.56 + /** 1.57 + * The map of XML elements that have been parsed. 1.58 + */ 1.59 + private Map<String,XMLNode> xmlElementsMap; 1.60 + private XMLNode currentNode; 1.61 + private final Configuration configuration; 1.62 + private String currentRoot; 1.63 + private boolean isParsing; 1.64 + 1.65 + private LayoutParser(Configuration configuration) { 1.66 + xmlElementsMap = new HashMap<String,XMLNode>(); 1.67 + this.configuration = configuration; 1.68 + } 1.69 + 1.70 + /** 1.71 + * Return an instance of the BuilderXML. 1.72 + * 1.73 + * @param configuration the current configuration of the doclet. 1.74 + * @return an instance of the BuilderXML. 1.75 + */ 1.76 + public static LayoutParser getInstance(Configuration configuration) { 1.77 + return new LayoutParser(configuration); 1.78 + } 1.79 + 1.80 + /** 1.81 + * Parse the XML specifying the layout of the documentation. 1.82 + * 1.83 + * @return the list of XML elements parsed. 1.84 + */ 1.85 + public XMLNode parseXML(String root) { 1.86 + if (xmlElementsMap.containsKey(root)) { 1.87 + return xmlElementsMap.get(root); 1.88 + } 1.89 + try { 1.90 + currentRoot = root; 1.91 + isParsing = false; 1.92 + SAXParserFactory factory = SAXParserFactory.newInstance(); 1.93 + SAXParser saxParser = factory.newSAXParser(); 1.94 + InputStream in = configuration.getBuilderXML(); 1.95 + saxParser.parse(in, this); 1.96 + return xmlElementsMap.get(root); 1.97 + } catch (Throwable t) { 1.98 + t.printStackTrace(); 1.99 + throw new DocletAbortException(t); 1.100 + } 1.101 + } 1.102 + 1.103 + /** 1.104 + * {@inheritDoc} 1.105 + */ 1.106 + @Override 1.107 + public void startElement(String namespaceURI, String sName, String qName, 1.108 + Attributes attrs) 1.109 + throws SAXException { 1.110 + if (isParsing || qName.equals(currentRoot)) { 1.111 + isParsing = true; 1.112 + currentNode = new XMLNode(currentNode, qName); 1.113 + for (int i = 0; i < attrs.getLength(); i++) 1.114 + currentNode.attrs.put(attrs.getLocalName(i), attrs.getValue(i)); 1.115 + if (qName.equals(currentRoot)) 1.116 + xmlElementsMap.put(qName, currentNode); 1.117 + } 1.118 + } 1.119 + 1.120 + /** 1.121 + * {@inheritDoc} 1.122 + */ 1.123 + @Override 1.124 + public void endElement(String namespaceURI, String sName, String qName) 1.125 + throws SAXException { 1.126 + if (! isParsing) { 1.127 + return; 1.128 + } 1.129 + currentNode = currentNode.parent; 1.130 + isParsing = ! qName.equals(currentRoot); 1.131 + } 1.132 +}