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 Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,139 @@ 1.4 +/* 1.5 + * Copyright 2003 Sun Microsystems, Inc. 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. Sun designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.26 + * have any questions. 1.27 + */ 1.28 +package com.sun.tools.doclets.internal.toolkit.builders; 1.29 + 1.30 +import com.sun.tools.doclets.internal.toolkit.*; 1.31 +import com.sun.tools.doclets.internal.toolkit.util.*; 1.32 +import java.io.*; 1.33 +import java.util.*; 1.34 +import org.xml.sax.*; 1.35 +import org.xml.sax.helpers.DefaultHandler; 1.36 +import javax.xml.parsers.*; 1.37 + 1.38 +/** 1.39 + * Parse the XML that specified the order of operation for the builders. This 1.40 + * Parser uses SAX parsing. 1.41 + * 1.42 + * @author Jamie Ho 1.43 + * @since 1.5 1.44 + * @see SAXParser 1.45 + */ 1.46 +public class LayoutParser extends DefaultHandler { 1.47 + 1.48 + /** 1.49 + * The map of XML elements that have been parsed. 1.50 + */ 1.51 + private Map xmlElementsMap; 1.52 + 1.53 + private Configuration configuration; 1.54 + private static LayoutParser instance; 1.55 + private String currentRoot; 1.56 + private boolean isParsing; 1.57 + 1.58 + /** 1.59 + * This class is a singleton. 1.60 + */ 1.61 + private LayoutParser(Configuration configuration) { 1.62 + xmlElementsMap = new HashMap(); 1.63 + this.configuration = configuration; 1.64 + } 1.65 + 1.66 + /** 1.67 + * Return an instance of the BuilderXML. 1.68 + * 1.69 + * @param configuration the current configuration of the doclet. 1.70 + * @return an instance of the BuilderXML. 1.71 + */ 1.72 + public static LayoutParser getInstance(Configuration configuration) { 1.73 + if (instance == null) { 1.74 + instance = new LayoutParser(configuration); 1.75 + } 1.76 + return instance; 1.77 + } 1.78 + 1.79 + /** 1.80 + * Parse the XML specifying the layout of the documentation. 1.81 + * 1.82 + * @return List the list of XML elements parsed. 1.83 + */ 1.84 + public List parseXML(String root) { 1.85 + if (xmlElementsMap.containsKey(root)) { 1.86 + return (List) xmlElementsMap.get(root); 1.87 + } 1.88 + try { 1.89 + List xmlElements = new ArrayList(); 1.90 + xmlElementsMap.put(root, xmlElements); 1.91 + currentRoot = root; 1.92 + isParsing = false; 1.93 + SAXParserFactory factory = SAXParserFactory.newInstance(); 1.94 + SAXParser saxParser = factory.newSAXParser(); 1.95 + InputStream in = configuration.getBuilderXML(); 1.96 + saxParser.parse(in, this); 1.97 + return xmlElements; 1.98 + } catch (Throwable t) { 1.99 + t.printStackTrace(); 1.100 + throw new DocletAbortException(); 1.101 + } 1.102 + } 1.103 + 1.104 + /** 1.105 + * {@inheritDoc} 1.106 + */ 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 + List xmlElements = (List) xmlElementsMap.get(currentRoot); 1.113 + xmlElements.add(qName); 1.114 + } 1.115 + } 1.116 + 1.117 + /** 1.118 + * {@inheritDoc} 1.119 + */ 1.120 + public void endElement(String namespaceURI, String sName, String qName) 1.121 + throws SAXException { 1.122 + if (! isParsing) { 1.123 + isParsing = false; 1.124 + return; 1.125 + } 1.126 + List xmlElements = (List) xmlElementsMap.get(currentRoot); 1.127 + if (xmlElements.get(xmlElements.size()-1).equals(qName)) { 1.128 + return; 1.129 + } else { 1.130 + List subElements = new ArrayList(); 1.131 + int targetIndex = xmlElements.indexOf(qName); 1.132 + int size = xmlElements.size(); 1.133 + for (int i = targetIndex; i < size; i++) { 1.134 + subElements.add(xmlElements.get(targetIndex)); 1.135 + xmlElements.remove(targetIndex); 1.136 + } 1.137 + //Save the sub elements as a list. 1.138 + xmlElements.add(subElements); 1.139 + } 1.140 + isParsing = ! qName.equals(currentRoot); 1.141 + } 1.142 +}