001    /*
002     * Copyright 2003-2008 Sun Microsystems, Inc.  All Rights Reserved.
003     * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004     *
005     * This code is free software; you can redistribute it and/or modify it
006     * under the terms of the GNU General Public License version 2 only, as
007     * published by the Free Software Foundation.  Sun designates this
008     * particular file as subject to the "Classpath" exception as provided
009     * by Sun in the LICENSE file that accompanied this code.
010     *
011     * This code is distributed in the hope that it will be useful, but WITHOUT
012     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013     * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014     * version 2 for more details (a copy is included in the LICENSE file that
015     * accompanied this code).
016     *
017     * You should have received a copy of the GNU General Public License version
018     * 2 along with this work; if not, write to the Free Software Foundation,
019     * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020     *
021     * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022     * CA 95054 USA or visit www.sun.com if you need additional information or
023     * have any questions.
024     */
025    package com.sun.tools.doclets.internal.toolkit.builders;
026    
027    import com.sun.tools.doclets.internal.toolkit.*;
028    import com.sun.tools.doclets.internal.toolkit.util.*;
029    import java.io.*;
030    import java.util.*;
031    import org.xml.sax.*;
032    import org.xml.sax.helpers.DefaultHandler;
033    import javax.xml.parsers.*;
034    
035    /**
036     * Parse the XML that specified the order of operation for the builders.  This
037     * Parser uses SAX parsing.
038     *
039     * @author Jamie Ho
040     * @since 1.5
041     * @see SAXParser
042     */
043    public class LayoutParser extends DefaultHandler {
044    
045        /**
046         * The map of XML elements that have been parsed.
047         */
048        private Map<String,List<Object>> xmlElementsMap;
049    
050        private Configuration configuration;
051        private static LayoutParser instance;
052        private String currentRoot;
053        private boolean isParsing;
054    
055        /**
056         * This class is a singleton.
057         */
058        private LayoutParser(Configuration configuration) {
059            xmlElementsMap = new HashMap<String,List<Object>>();
060            this.configuration = configuration;
061        }
062    
063        /**
064         * Return an instance of the BuilderXML.
065         *
066         * @param configuration the current configuration of the doclet.
067         * @return an instance of the BuilderXML.
068         */
069        public static LayoutParser getInstance(Configuration configuration) {
070            if (instance == null) {
071                instance = new LayoutParser(configuration);
072            }
073            return instance;
074        }
075    
076        /**
077         * Parse the XML specifying the layout of the documentation.
078         *
079         * @return List the list of XML elements parsed.
080         */
081        public List<?> parseXML(String root) {
082            if (xmlElementsMap.containsKey(root)) {
083                return xmlElementsMap.get(root);
084            }
085            try {
086                List<Object> xmlElements = new ArrayList<Object>();
087                xmlElementsMap.put(root, xmlElements);
088                currentRoot = root;
089                isParsing = false;
090                SAXParserFactory factory = SAXParserFactory.newInstance();
091                SAXParser saxParser = factory.newSAXParser();
092                InputStream in = configuration.getBuilderXML();
093                saxParser.parse(in, this);
094                return xmlElements;
095            } catch (Throwable t) {
096                t.printStackTrace();
097                throw new DocletAbortException();
098            }
099        }
100    
101        /**
102         * {@inheritDoc}
103         */
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                List<Object> xmlElements = xmlElementsMap.get(currentRoot);
110                xmlElements.add(qName);
111            }
112        }
113    
114        /**
115         * {@inheritDoc}
116         */
117        public void endElement(String namespaceURI, String sName, String qName)
118        throws SAXException {
119            if (! isParsing) {
120                isParsing = false;
121                return;
122            }
123            List<Object> xmlElements = xmlElementsMap.get(currentRoot);
124            if (xmlElements.get(xmlElements.size()-1).equals(qName)) {
125                return;
126            } else {
127                List<Object> subElements = new ArrayList<Object>();
128                int targetIndex = xmlElements.indexOf(qName);
129                int size = xmlElements.size();
130                for (int i = targetIndex; i < size; i++) {
131                    subElements.add(xmlElements.get(targetIndex));
132                    xmlElements.remove(targetIndex);
133                }
134                //Save the sub elements as a list.
135                xmlElements.add(subElements);
136            }
137            isParsing = ! qName.equals(currentRoot);
138        }
139    }