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

Tue, 28 Dec 2010 15:54:52 -0800

author
ohair
date
Tue, 28 Dec 2010 15:54:52 -0800
changeset 798
4868a36f6fd8
parent 766
90af8d87741f
child 1357
c75be5bc5283
permissions
-rw-r--r--

6962318: Update copyright year
Reviewed-by: xdono

     1 /*
     2  * Copyright (c) 2003, 2010, 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;
    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,XMLNode> xmlElementsMap;
    49     private XMLNode currentNode;
    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,XMLNode>();
    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 the list of XML elements parsed.
    80      */
    81     public XMLNode parseXML(String root) {
    82         if (xmlElementsMap.containsKey(root)) {
    83             return xmlElementsMap.get(root);
    84         }
    85         try {
    86             currentRoot = root;
    87             isParsing = false;
    88             SAXParserFactory factory = SAXParserFactory.newInstance();
    89             SAXParser saxParser = factory.newSAXParser();
    90             InputStream in = configuration.getBuilderXML();
    91             saxParser.parse(in, this);
    92             return xmlElementsMap.get(root);
    93         } catch (Throwable t) {
    94             t.printStackTrace();
    95             throw new DocletAbortException();
    96         }
    97     }
    99     /**
   100      * {@inheritDoc}
   101      */
   102     @Override
   103     public void startElement(String namespaceURI, String sName, String qName,
   104         Attributes attrs)
   105     throws SAXException {
   106         if (isParsing || qName.equals(currentRoot)) {
   107             isParsing = true;
   108             currentNode = new XMLNode(currentNode, qName);
   109             for (int i = 0; i < attrs.getLength(); i++)
   110                 currentNode.attrs.put(attrs.getLocalName(i), attrs.getValue(i));
   111             if (qName.equals(currentRoot))
   112                 xmlElementsMap.put(qName, currentNode);
   113         }
   114     }
   116     /**
   117      * {@inheritDoc}
   118      */
   119     @Override
   120     public void endElement(String namespaceURI, String sName, String qName)
   121     throws SAXException {
   122         if (! isParsing) {
   123             return;
   124         }
   125         currentNode = currentNode.parent;
   126         isParsing = ! qName.equals(currentRoot);
   127     }
   128 }

mercurial