KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml2 > QElementDef


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xml2;
30
31 import org.w3c.dom.Node JavaDoc;
32
33 import javax.xml.namespace.QName JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.util.ArrayList JavaDoc;
36
37 class QElementDef extends QNode {
38   String JavaDoc _name;
39   Object JavaDoc _content;
40   ArrayList JavaDoc<QAttributeDef> _attr;
41   boolean _hasDefault;
42   QDocumentType _dtd;
43
44   QElementDef(String JavaDoc name)
45   {
46     _name = name;
47   }
48
49   public String JavaDoc getNodeName() { return "#element"; }
50   public String JavaDoc getTagName() { return "#element"; }
51   public short getNodeType() { return Node.ELEMENT_NODE; }
52
53   Node JavaDoc importNode(QDocument owner, boolean deep)
54   {
55     QElementDef def = new QElementDef(_name);
56
57     return def;
58   }
59
60   public void addAttribute(String JavaDoc name, String JavaDoc type, ArrayList JavaDoc enumeration,
61                String JavaDoc qualifier, String JavaDoc deflt)
62   {
63     if (_attr == null)
64       _attr = new ArrayList JavaDoc<QAttributeDef>();
65
66     if (deflt != null) {
67       _hasDefault = true;
68       _dtd.setAttributeDefaults();
69     }
70     
71     _attr.add(new QAttributeDef(name, type, enumeration, qualifier, deflt));
72   }
73
74   void fillDefaults(QElement element)
75   {
76     if (! _hasDefault)
77       return;
78
79     for (int i = 0; i < _attr.size(); i++) {
80       QAttributeDef attrDef = _attr.get(i);
81       if (attrDef._deflt != null &&
82       element.getAttribute(attrDef._name).equals("")) {
83     QAttr attr = (QAttr) element._owner.createAttribute(attrDef._name,
84                                 attrDef._deflt);
85     attr._owner = element._owner;
86     attr.setSpecified(false);
87     element.setAttributeNode(attr);
88       }
89     }
90   }
91
92   void fillDefaults(QAttributes attributes)
93   {
94     if (! _hasDefault)
95       return;
96
97     for (int i = 0; i < _attr.size(); i++) {
98       QAttributeDef attrDef = _attr.get(i);
99       if (attrDef._deflt != null &&
100       attributes.getIndex(attrDef._name) < 0) {
101     attributes.add(new QName JavaDoc(null, attrDef._name, null), attrDef._deflt);
102       }
103     }
104   }
105
106   public void print(XmlPrinter os) throws IOException JavaDoc
107   {
108     if (_content != null) {
109       os.print("<!ELEMENT ");
110       os.print(_name);
111       os.print(" ");
112       if (_content instanceof QContentParticle)
113     ((QContentParticle) _content).print(os);
114       else
115     os.print(String.valueOf(_content));
116       os.println(">");
117     }
118
119     if (_attr != null) {
120       os.print("<!ATTLIST ");
121       os.print(_name);
122
123       for (int i = 0; i < _attr.size(); i++) {
124     QAttributeDef attribute = _attr.get(i);
125
126     if (_attr.size() == 1)
127       os.print(" ");
128     else
129       os.print("\n ");
130     os.print(attribute._name);
131     if (attribute._type.equals("#ENUM")) {
132       os.print(" (");
133       for (int j = 0; j < attribute._enumeration.size(); j++) {
134         String JavaDoc enumType = attribute._enumeration.get(j);
135
136         if (j != 0)
137           os.print(" | ");
138         os.print(enumType);
139       }
140       os.print(")");
141     } else if (attribute._type.equals("NOTATION")) {
142       os.print(" NOTATION (");
143       for (int j = 0; j < attribute._enumeration.size(); j++) {
144         String JavaDoc enumType = attribute._enumeration.get(j);
145
146         if (j != 0)
147           os.print(" | ");
148         os.print(enumType);
149       }
150       os.print(")");
151     } else {
152       os.print(" ");
153       os.print(attribute._type);
154     }
155
156     if (attribute._qualifier != null) {
157       os.print(" ");
158       os.print(attribute._qualifier);
159     }
160     if (attribute._deflt != null) {
161       os.print(" \"");
162       os.print(attribute._deflt);
163       os.print("\"");
164     }
165       }
166       os.println(">");
167     }
168   }
169 }
170
Popular Tags