KickJava   Java API By Example, From Geeks To Geeks.

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