fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * The contents of this file are subject to the terms
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * of the Common Development and Distribution License
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * (the License). You may not use this file except in
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * compliance with the License.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * You can obtain a copy of the License at
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * https://opensso.dev.java.net/public/CDDLv1.0.html or
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * See the License for the specific language governing
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * permission and limitations under the License.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * When distributing Covered Code, include this CDDL
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Header Notice in each file and include the License file
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * If applicable, add the following below the CDDL Header,
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * with the fields enclosed by brackets [] replaced by
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * your own identifying information:
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * "Portions Copyrighted [year] [name of copyright owner]"
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * $Id: XMLParser.java,v 1.2 2008/06/25 05:51:32 qcheng Exp $
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * This is a simple parser that can be used to parse and create in-memory
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * DOM-like representation of an xml document. The logic for this parser is
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * based on an adhoc implementation which requires that the XML document be
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * well formed syntactically and does not do any further validation of any
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Majorpublic class XMLParser implements IXMLUtilsConstants {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * This method returns an ordered colloection of tokens that can together
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * represent the entire XML document. This method is used as the first
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * phase parsing or scanning of the XML document.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param isreader
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the reader attached to the source of the document.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return an ordered collection of <code>Token</code>s that together
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * represent the entire XML document.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * if the parse was not successful
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major ArrayList parse(Reader isreader) throws Exception {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major boolean inComment = false;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major boolean inCompoundToken = false;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major boolean inQuotes = false;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major BufferedReader in = new BufferedReader(isreader);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if ((buff.length() != 0) && buff.toString().endsWith("\\")) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Invalid boundCount: " + boundCount);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major && buff.substring(buff.length() - 2, buff.length())
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Char > in begining of token");
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (buff.length() > 2 && buff.toString().startsWith("<!-")) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("unbalanced quote encountered");
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("unbalanced token encountered: boundCount = "
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception(" invalid boundCount: " + boundCount);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result.add(getXMLTokenFactory().getToken(buff.toString()));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * This method is used as the second phase parser to create an ordered
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * collection of attributes from a given attribute string.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param attributeString
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the attribute string fragment to be parsed
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return an ordered collection of attributes
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * if the parse was not successful
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major ArrayList parseAttributes(String attributeString) throws Exception {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (attributeString != null && attributeString.trim().length() > 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major boolean inQuotes = false;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major boolean inName = true;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major boolean inValue = false;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major for (int i = 0; i < attributeString.length(); i++) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if ((buff.length() != 0) && buff.toString().endsWith("\\"))
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (name == null || name.trim().length() == 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Failed to parse attribute: "
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result.add(new XMLElementAttribute(name, value));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Failed to parse attributes: "
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (name == null || name.trim().length() == 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Failed to parse attribute: "
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result.add(new XMLElementAttribute(name, value));
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major return this.getXMLTokenFactory().getNextTokenIndex();