KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > xb > binding > metadata > XsdElement


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.xb.binding.metadata;
23
24 import java.util.HashMap JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Map JavaDoc;
27 import javax.xml.namespace.QName JavaDoc;
28
29 /**
30  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
31  * @version <tt>$Revision: 1958 $</tt>
32  */

33 public class XsdElement
34 {
35    public static final QName JavaDoc QNAME_NAME = new QName JavaDoc("name");
36
37    private final QName JavaDoc qName;
38    private Map JavaDoc attributes = Collections.EMPTY_MAP;
39    private Map JavaDoc children = Collections.EMPTY_MAP;
40    private String JavaDoc data;
41
42    public XsdElement(QName JavaDoc qName)
43    {
44       this.qName = qName;
45    }
46
47    public String JavaDoc getAttribute(QName JavaDoc qName)
48    {
49       return (String JavaDoc)attributes.get(qName);
50    }
51
52    public void addAttribute(QName JavaDoc qName, String JavaDoc value)
53    {
54       switch(attributes.size())
55       {
56          case 0:
57             attributes = Collections.singletonMap(qName, value);
58             break;
59          case 1:
60             attributes = new HashMap JavaDoc(attributes);
61          default:
62             attributes.put(qName, value);
63       }
64    }
65
66    public XsdElement getChild(QName JavaDoc qName)
67    {
68       return (XsdElement)children.get(qName);
69    }
70
71    public void addChild(XsdElement child)
72    {
73       switch(children.size())
74       {
75          case 0:
76             children = Collections.singletonMap(child.qName, child);
77             break;
78          case 1:
79             children = new HashMap JavaDoc(children);
80          default:
81             children.put(child.qName, child);
82       }
83    }
84
85    public String JavaDoc getData()
86    {
87       return data;
88    }
89
90    public void setData(String JavaDoc data)
91    {
92       this.data = data;
93    }
94
95    public QName JavaDoc getQName()
96    {
97       return qName;
98    }
99
100    public String JavaDoc getNameAttribute()
101    {
102       return getAttribute(QNAME_NAME);
103    }
104
105    public void setNameAttribute(String JavaDoc name)
106    {
107       addAttribute(QNAME_NAME, name);
108    }
109    
110    public boolean equals(Object JavaDoc o)
111    {
112       if(this == o)
113       {
114          return true;
115       }
116       if(!(o instanceof XsdElement))
117       {
118          return false;
119       }
120
121       final XsdElement xsdElement = (XsdElement)o;
122
123       if(!qName.equals(xsdElement.qName))
124       {
125          return false;
126       }
127
128       return true;
129    }
130
131    public int hashCode()
132    {
133       return qName.hashCode();
134    }
135 }
136
Popular Tags