001: /*
002: * Redistribution and use of this software and associated documentation
003: * ("Software"), with or without modification, are permitted provided
004: * that the following conditions are met:
005: *
006: * 1. Redistributions of source code must retain copyright
007: * statements and notices. Redistributions must also contain a
008: * copy of this document.
009: *
010: * 2. Redistributions in binary form must reproduce the
011: * above copyright notice, this list of conditions and the
012: * following disclaimer in the documentation and/or other
013: * materials provided with the distribution.
014: *
015: * 3. The name "Exolab" must not be used to endorse or promote
016: * products derived from this Software without prior written
017: * permission of Intalio, Inc. For written permission,
018: * please contact info@exolab.org.
019: *
020: * 4. Products derived from this Software may not be called "Exolab"
021: * nor may "Exolab" appear in their names without prior written
022: * permission of Intalio, Inc. Exolab is a registered
023: * trademark of Intalio, Inc.
024: *
025: * 5. Due credit should be given to the Exolab Project
026: * (http://www.exolab.org/).
027: *
028: * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
029: * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
030: * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
031: * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
032: * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
033: * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
034: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
035: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
036: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
037: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
038: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
039: * OF THE POSSIBILITY OF SUCH DAMAGE.
040: *
041: * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
042: *
043: * $Id: NodeType.java 6761 2007-01-18 05:57:59Z ekuns $
044: */
045: package org.exolab.castor.xml;
046:
047: /**
048: * The possible node types for an XML field. A field can be represented as an
049: * attribute, an element or text content. The default is attribute. This class
050: * is essentially a typesafe enumeration and the instances are immutable.
051: *
052: * @author <a href="arkin@intalio.com">Assaf Arkin</a>
053: * @author <a href="kvisco@intalio.com">Keith Visco</a>
054: * @version $Revision: 6761 $ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
055: */
056: public final class NodeType {
057:
058: /** The attribute type. */
059: public static final short ATTRIBUTE = 0;
060: /** The element type. */
061: public static final short ELEMENT = 1;
062: /** The namespace node type. */
063: public static final short NAMESPACE = 2;
064: /** The text type. */
065: public static final short TEXT = 3;
066: /** Attribute node type (<tt>attribute</tt>). This field will appear in
067: * the XML document as an element's attribute. */
068: public static final NodeType Attribute = new NodeType(
069: NodeType.ATTRIBUTE, "attribute");
070: /** Element node type (<tt>element</tt>). This field will appear in the
071: * XML document as a contained element. */
072: public static final NodeType Element = new NodeType(
073: NodeType.ELEMENT, "element");
074: /** Namespace node type (<tt>namespace</tt>). This field will appear in
075: * the XML document as a namespace declaration. */
076: public static final NodeType Namespace = new NodeType(
077: NodeType.NAMESPACE, "namespace");
078: /** Content node type (<tt>text</tt>). This field will appear in the XML
079: * document as the element text content. */
080: public static final NodeType Text = new NodeType(NodeType.TEXT,
081: "text");
082:
083: /** The name of this node type as it would appear in a mapping file. */
084: private final String _name;
085: /** The type of this NodeType. */
086: private final short _type;
087:
088: /**
089: * Private constructor ... creates a new NodeType.
090: * @param type Type of node
091: * @param name Name for the node
092: */
093: private NodeType(final short type, final String name) {
094: _type = type;
095: _name = name;
096: }
097:
098: /**
099: * Returns the node type from the name. If <tt>nodeType</tt> is null,
100: * return the default node type ({@link #Attribute}). Otherwise returns
101: * the named node type mode.
102: *
103: * @param nodeType The node type name
104: * @return The node type
105: */
106: public static NodeType getNodeType(final String nodeType) {
107: if (nodeType == null) {
108: return Attribute;
109: }
110: if (nodeType.equals(Attribute._name)) {
111: return Attribute;
112: }
113: if (nodeType.equals(Namespace._name)) {
114: return Namespace;
115: }
116: if (nodeType.equals(Element._name)) {
117: return Element;
118: }
119: if (nodeType.equals(Text._name)) {
120: return Text;
121: }
122: // We don't expect that this can happen without subclassing.
123: throw new IllegalArgumentException("Unrecognized node type");
124: }
125:
126: /**
127: * Returns the type of this NodeType.
128: * @return the type of this NodeType.
129: */
130: public short getType() {
131: return _type;
132: }
133:
134: /**
135: * Returns the name of this NodeType.
136: * @return the name of this NodeType.
137: */
138: public String toString() {
139: return _name;
140: }
141:
142: }
|