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

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 74
5a9172b251dd
child 184
905e151a185a
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

     1 /*
     2  * Copyright 2003-2008 Sun Microsystems, Inc.  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.  Sun designates this
     8  * particular file as subject to the "Classpath" exception as provided
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
    23  * have any questions.
    24  */
    25 package com.sun.tools.doclets.internal.toolkit.builders;
    27 import com.sun.tools.doclets.internal.toolkit.*;
    28 import com.sun.tools.doclets.internal.toolkit.util.*;
    29 import java.io.*;
    30 import java.util.*;
    31 import org.xml.sax.*;
    32 import org.xml.sax.helpers.DefaultHandler;
    33 import javax.xml.parsers.*;
    35 /**
    36  * Parse the XML that specified the order of operation for the builders.  This
    37  * Parser uses SAX parsing.
    38  *
    39  * @author Jamie Ho
    40  * @since 1.5
    41  * @see SAXParser
    42  */
    43 public class LayoutParser extends DefaultHandler {
    45     /**
    46      * The map of XML elements that have been parsed.
    47      */
    48     private Map<String,List<Object>> xmlElementsMap;
    50     private Configuration configuration;
    51     private static LayoutParser instance;
    52     private String currentRoot;
    53     private boolean isParsing;
    55     /**
    56      * This class is a singleton.
    57      */
    58     private LayoutParser(Configuration configuration) {
    59         xmlElementsMap = new HashMap<String,List<Object>>();
    60         this.configuration = configuration;
    61     }
    63     /**
    64      * Return an instance of the BuilderXML.
    65      *
    66      * @param configuration the current configuration of the doclet.
    67      * @return an instance of the BuilderXML.
    68      */
    69     public static LayoutParser getInstance(Configuration configuration) {
    70         if (instance == null) {
    71             instance = new LayoutParser(configuration);
    72         }
    73         return instance;
    74     }
    76     /**
    77      * Parse the XML specifying the layout of the documentation.
    78      *
    79      * @return List the list of XML elements parsed.
    80      */
    81     public List parseXML(String root) {
    82         if (xmlElementsMap.containsKey(root)) {
    83             return (List) xmlElementsMap.get(root);
    84         }
    85         try {
    86             List<Object> xmlElements = new ArrayList<Object>();
    87             xmlElementsMap.put(root, xmlElements);
    88             currentRoot = root;
    89             isParsing = false;
    90             SAXParserFactory factory = SAXParserFactory.newInstance();
    91             SAXParser saxParser = factory.newSAXParser();
    92             InputStream in = configuration.getBuilderXML();
    93             saxParser.parse(in, this);
    94             return xmlElements;
    95         } catch (Throwable t) {
    96             t.printStackTrace();
    97             throw new DocletAbortException();
    98         }
    99     }
   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     }
   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 }

mercurial