fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major/**
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major *
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major *
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 *
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 * opensso/legal/CDDLv1.0.txt
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * See the License for the specific language governing
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * permission and limitations under the License.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major *
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * When distributing Covered Code, include this CDDL
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Header Notice in each file and include the License file
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * at opensso/legal/CDDLv1.0.txt.
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 *
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * $Id: XMLTokenFactory.java,v 1.2 2008/06/25 05:51:32 qcheng Exp $
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major *
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major */
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Majorpackage com.sun.identity.install.tools.util.xml;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major/**
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * A token factory is responsible for the creation of the associated token
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * handlers or <code>Token</code> objects given string fragments as parsed
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * from the XML document source. This factory is used by the parser to generate
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the tokens when strings matching the syntactic rules of XML are identified
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * during the first parsing or scanning phase. The token factory is also
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * responsible for assigning unique identification index numbers to tokens that
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * is used by the parser and later by the document itself in order to create
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * new elements for addition as necessary.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major */
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Majorpublic class XMLTokenFactory implements IXMLUtilsConstants {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major /**
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Returns a <code>Token</code> handler which can be used to model the
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * given token string.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major *
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @param tokenString
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the string fragment that needs to be processed for the second
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * phase of parsing
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major *
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return a <code>Token</code> object which is nothing but a handler for
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the given string fragment which can be used to further handle
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * the given string framgment.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major *
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @throws Exception
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * if the identification of the appropriate token handler
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * fails.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major */
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major Token getToken(String tokenString) throws Exception {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major Token result = null;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (tokenString != null) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (tokenString.startsWith("<") && tokenString.endsWith(">")) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (tokenString.startsWith("<!")) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (tokenString.startsWith("<!--")
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major && tokenString.endsWith("-->")) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result = new CommentToken(tokenString);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major } else {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major String innerString = tokenString.substring(2,
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major tokenString.length() - 1);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (innerString.trim().startsWith(DOCTYPE)) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result = new DoctypeToken(tokenString);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major } else {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result = new MetaToken(tokenString);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major }
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major }
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major } else if (tokenString.startsWith("<?")) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result = new MetaToken(tokenString);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major } else {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result = new BoundedToken(tokenString);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major }
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major } else if (!tokenString.startsWith("<")
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major && !tokenString.endsWith(">")) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (tokenString.trim().length() == 0) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result = new WhiteSpaceToken(tokenString);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major } else {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result = new UnboundedToken(tokenString);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major }
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major } else {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Invalid token String: " + tokenString);
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major }
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major } else {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major throw new Exception("Null token string");
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major }
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major if (result != null) {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major result.setTokenIndex(getNextTokenIndex());
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major }
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major return result;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major }
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major /**
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * Assigns a unique index number for the next token handler.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major *
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * @return a unique index used for identifying the token in the actual
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major * source document.
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major */
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major int getNextTokenIndex() {
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major return tokenIndex++;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major }
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major private int tokenIndex = 0;
fb379c70e3fd8a537f311b99be4759ae41e02750Peter Major}