View Javadoc
1   /*
2    * Redistribution and use of this software and associated documentation ("Software"), with or
3    * without modification, are permitted provided that the following conditions are met:
4    *
5    * 1. Redistributions of source code must retain copyright statements and notices. Redistributions
6    * must also contain a copy of this document.
7    *
8    * 2. Redistributions in binary form must reproduce the above copyright notice, this list of
9    * conditions and the following disclaimer in the documentation and/or other materials provided with
10   * the distribution.
11   *
12   * 3. The name "Exolab" must not be used to endorse or promote products derived from this Software
13   * without prior written permission of Intalio, Inc. For written permission, please contact
14   * info@exolab.org.
15   *
16   * 4. Products derived from this Software may not be called "Exolab" nor may "Exolab" appear in
17   * their names without prior written permission of Intalio, Inc. Exolab is a registered trademark of
18   * Intalio, Inc.
19   *
20   * 5. Due credit should be given to the Exolab Project (http://www.exolab.org/).
21   *
22   * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESSED OR
23   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
24   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTALIO, INC. OR ITS
25   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
29   * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30   *
31   * Copyright 1999-2002 (C) Intalio, Inc. All Rights Reserved.
32   *
33   * $Id$
34   */
35  package org.exolab.castor.xml;
36  
37  /**
38   * The possible node types for an XML field. A field can be represented as an attribute, an element
39   * or text content. The default is attribute. This class is essentially a typesafe enumeration and
40   * the instances are immutable.
41   *
42   * @author <a href="arkin@intalio.com">Assaf Arkin</a>
43   * @author <a href="kvisco@intalio.com">Keith Visco</a>
44   * @version $Revision$ $Date: 2003-03-03 00:05:44 -0700 (Mon, 03 Mar 2003) $
45   */
46  public final class NodeType {
47  
48    /** The attribute type. */
49    public static final short ATTRIBUTE = 0;
50    /** The element type. */
51    public static final short ELEMENT = 1;
52    /** The namespace node type. */
53    public static final short NAMESPACE = 2;
54    /** The text type. */
55    public static final short TEXT = 3;
56    /**
57     * Attribute node type (<tt>attribute</tt>). This field will appear in the XML document as an
58     * element's attribute.
59     */
60    public static final NodeType Attribute = new NodeType(NodeType.ATTRIBUTE, "attribute");
61    /**
62     * Element node type (<tt>element</tt>). This field will appear in the XML document as a contained
63     * element.
64     */
65    public static final NodeType Element = new NodeType(NodeType.ELEMENT, "element");
66    /**
67     * Namespace node type (<tt>namespace</tt>). This field will appear in the XML document as a
68     * namespace declaration.
69     */
70    public static final NodeType Namespace = new NodeType(NodeType.NAMESPACE, "namespace");
71    /**
72     * Content node type (<tt>text</tt>). This field will appear in the XML document as the element
73     * text content.
74     */
75    public static final NodeType Text = new NodeType(NodeType.TEXT, "text");
76  
77    /** The name of this node type as it would appear in a mapping file. */
78    private final String _name;
79    /** The type of this NodeType. */
80    private final short _type;
81  
82    /**
83     * Private constructor ... creates a new NodeType.
84     * 
85     * @param type Type of node
86     * @param name Name for the node
87     */
88    private NodeType(final short type, final String name) {
89      _type = type;
90      _name = name;
91    }
92  
93    /**
94     * Returns the node type from the name. If <tt>nodeType</tt> is null, return the default node type
95     * ({@link #Attribute}). Otherwise returns the named node type mode.
96     *
97     * @param nodeType The node type name
98     * @return The node type
99     */
100   public static NodeType getNodeType(final String nodeType) {
101     if (nodeType == null) {
102       return Attribute;
103     }
104     if (nodeType.equals(Attribute._name)) {
105       return Attribute;
106     }
107     if (nodeType.equals(Namespace._name)) {
108       return Namespace;
109     }
110     if (nodeType.equals(Element._name)) {
111       return Element;
112     }
113     if (nodeType.equals(Text._name)) {
114       return Text;
115     }
116     // We don't expect that this can happen without subclassing.
117     throw new IllegalArgumentException("Unrecognized node type");
118   }
119 
120   /**
121    * Returns the type of this NodeType.
122    * 
123    * @return the type of this NodeType.
124    */
125   public short getType() {
126     return _type;
127   }
128 
129   /**
130    * Returns the name of this NodeType.
131    * 
132    * @return the name of this NodeType.
133    */
134   public String toString() {
135     return _name;
136   }
137 
138 }