XMLTokenFactory.java revision fb379c70e3fd8a537f311b99be4759ae41e02750
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync/**
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync *
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * Copyright (c) 2006 Sun Microsystems Inc. All Rights Reserved
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync *
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * The contents of this file are subject to the terms
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * of the Common Development and Distribution License
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * (the License). You may not use this file except in
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * compliance with the License.
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync *
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * You can obtain a copy of the License at
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * https://opensso.dev.java.net/public/CDDLv1.0.html or
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * opensso/legal/CDDLv1.0.txt
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * See the License for the specific language governing
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * permission and limitations under the License.
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync *
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * When distributing Covered Code, include this CDDL
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * Header Notice in each file and include the License file
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * at opensso/legal/CDDLv1.0.txt.
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * If applicable, add the following below the CDDL Header,
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * with the fields enclosed by brackets [] replaced by
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * your own identifying information:
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * "Portions Copyrighted [year] [name of copyright owner]"
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync *
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync * $Id: XMLTokenFactory.java,v 1.2 2008/06/25 05:51:32 qcheng Exp $
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync *
930b5f872e89407f445d4000d4e4aaecaa6a0998vboxsync */
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsyncpackage com.sun.identity.install.tools.util.xml;
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync/**
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * A token factory is responsible for the creation of the associated token
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * handlers or <code>Token</code> objects given string fragments as parsed
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * from the XML document source. This factory is used by the parser to generate
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * the tokens when strings matching the syntactic rules of XML are identified
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * during the first parsing or scanning phase. The token factory is also
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * responsible for assigning unique identification index numbers to tokens that
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * is used by the parser and later by the document itself in order to create
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * new elements for addition as necessary.
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync */
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsyncpublic class XMLTokenFactory implements IXMLUtilsConstants {
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync /**
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * Returns a <code>Token</code> handler which can be used to model the
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * given token string.
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync *
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * @param tokenString
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * the string fragment that needs to be processed for the second
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * phase of parsing
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync *
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * @return a <code>Token</code> object which is nothing but a handler for
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * the given string fragment which can be used to further handle
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * the given string framgment.
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync *
3194da424708abdd288b28d96892b3a5f3f7df0bvboxsync * @throws Exception
* if the identification of the appropriate token handler
* fails.
*/
Token getToken(String tokenString) throws Exception {
Token result = null;
if (tokenString != null) {
if (tokenString.startsWith("<") && tokenString.endsWith(">")) {
if (tokenString.startsWith("<!")) {
if (tokenString.startsWith("<!--")
&& tokenString.endsWith("-->")) {
result = new CommentToken(tokenString);
} else {
String innerString = tokenString.substring(2,
tokenString.length() - 1);
if (innerString.trim().startsWith(DOCTYPE)) {
result = new DoctypeToken(tokenString);
} else {
result = new MetaToken(tokenString);
}
}
} else if (tokenString.startsWith("<?")) {
result = new MetaToken(tokenString);
} else {
result = new BoundedToken(tokenString);
}
} else if (!tokenString.startsWith("<")
&& !tokenString.endsWith(">")) {
if (tokenString.trim().length() == 0) {
result = new WhiteSpaceToken(tokenString);
} else {
result = new UnboundedToken(tokenString);
}
} else {
throw new Exception("Invalid token String: " + tokenString);
}
} else {
throw new Exception("Null token string");
}
if (result != null) {
result.setTokenIndex(getNextTokenIndex());
}
return result;
}
/**
* Assigns a unique index number for the next token handler.
*
* @return a unique index used for identifying the token in the actual
* source document.
*/
int getNextTokenIndex() {
return tokenIndex++;
}
private int tokenIndex = 0;
}