KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > QDocumentType


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.xml;
31
32 import org.w3c.dom.DocumentType JavaDoc;
33 import org.w3c.dom.NamedNodeMap JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35
36 import java.io.IOException JavaDoc;
37 import java.util.HashMap JavaDoc;
38 import java.util.Iterator JavaDoc;
39
40 public class QDocumentType extends QNode implements DocumentType JavaDoc {
41   String JavaDoc _name;
42   HashMap JavaDoc<String JavaDoc,QElementDef> _elements = new HashMap JavaDoc<String JavaDoc,QElementDef>();
43   HashMap JavaDoc<String JavaDoc,QEntity> _entities = new HashMap JavaDoc<String JavaDoc,QEntity>();
44   HashMap JavaDoc<String JavaDoc,QNotation> _notations = new HashMap JavaDoc<String JavaDoc,QNotation>();
45   HashMap JavaDoc<String JavaDoc,QEntity> _parameterEntities = new HashMap JavaDoc<String JavaDoc,QEntity>();
46   HashMap JavaDoc<String JavaDoc,String JavaDoc> _ids = new HashMap JavaDoc<String JavaDoc,String JavaDoc>();
47   String JavaDoc _systemId;
48   String JavaDoc _publicId;
49
50   boolean _hasAttributeDefaults;
51
52   /**
53    * Create a new document type.
54    */

55   public QDocumentType(String JavaDoc name)
56   {
57     this(name, null, null);
58   }
59
60   public QDocumentType(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
61   {
62     _name = name;
63
64     _entities.put("amp", new QEntity("amp", "&"));
65     _entities.put("lt", new QEntity("lt", "<"));
66     _entities.put("gt", new QEntity("gt", ">"));
67     _entities.put("quot", new QEntity("quot", "\""));
68     _entities.put("apos", new QEntity("apos", "'"));
69
70     _publicId = publicId;
71     _systemId = systemId;
72   }
73
74   public String JavaDoc getNodeName() { return _name; }
75   public String JavaDoc getTagName() { return "#documenttype"; }
76   public short getNodeType() { return DOCUMENT_TYPE_NODE; }
77
78   public String JavaDoc getPrefix() { return null; }
79   public String JavaDoc getLocalName() { return null; }
80   public String JavaDoc getNamespaceURI() { return null; }
81
82   public String JavaDoc getName() { return _name; }
83   public void setName(String JavaDoc name) { _name = name; }
84   
85   public NamedNodeMap JavaDoc getEntities()
86   {
87     return new QNamedNodeMap(_entities);
88   }
89
90   public NamedNodeMap JavaDoc getNotations()
91   {
92     return new QNamedNodeMap(_notations);
93   }
94
95   public void setLocation(String JavaDoc filename, int line, int col)
96   {
97   }
98
99   Node JavaDoc importNode(QDocument owner, boolean deep)
100   {
101     QDocumentType ref = new QDocumentType(_name);
102
103     return ref;
104   }
105
106   void addNotation(QNotation notation)
107   {
108     _notations.put(notation._name, notation);
109   }
110
111   public String JavaDoc getElementId(String JavaDoc element)
112   {
113     return _ids.get(element);
114   }
115
116   public Iterator JavaDoc getElementIdNames()
117   {
118     return _ids.keySet().iterator();
119   }
120
121   void setElementId(String JavaDoc element, String JavaDoc id)
122   {
123     _ids.put(element, id);
124   }
125
126   /**
127    * Adds a new defined entity.
128    */

129   void addEntity(QEntity entity)
130   {
131     if (_entities.get(entity._name) == null)
132       _entities.put(entity._name, entity);
133   }
134
135   QEntity getEntity(String JavaDoc name)
136   {
137     return _entities.get(name);
138   }
139
140   void addParameterEntity(QEntity entity)
141   {
142     if (_parameterEntities.get(entity._name) == null)
143       _parameterEntities.put(entity._name, entity);
144   }
145
146   QEntity getParameterEntity(String JavaDoc name)
147   {
148     return _parameterEntities.get(name);
149   }
150
151   String JavaDoc getEntityValue(String JavaDoc name)
152   {
153     QEntity entity = _entities.get(name);
154
155     if (entity == null)
156       return null;
157     else
158       return entity._value;
159   }
160
161   public String JavaDoc getSystemId()
162   {
163     return _systemId;
164   }
165
166   protected void setSystemId(String JavaDoc systemId)
167   {
168     _systemId = systemId;
169   }
170
171   public String JavaDoc getPublicId()
172   {
173     return _publicId;
174   }
175
176   protected void setPublicId(String JavaDoc publicId)
177   {
178     _publicId = publicId;
179   }
180
181   public String JavaDoc getInternalSubset()
182   {
183     return null;
184   }
185   
186   boolean isExternal()
187   {
188     return _systemId != null || _publicId != null;
189   }
190
191   public QElementDef getElement(String JavaDoc name)
192   {
193     return _elements.get(name);
194   }
195
196   QElementDef addElement(String JavaDoc name)
197   {
198     QElementDef def = _elements.get(name);
199     if (def == null) {
200       def = new QElementDef(name);
201       def._dtd = this;
202       def._owner = _owner;
203       _elements.put(name, def);
204       appendChild(def);
205     }
206
207     return def;
208   }
209
210   void setAttributeDefaults()
211   {
212     _hasAttributeDefaults = true;
213   }
214
215   boolean hasAttributeDefaults()
216   {
217     return _hasAttributeDefaults;
218   }
219
220   void fillDefaults(QElement element)
221   {
222     if (! _hasAttributeDefaults)
223       return;
224
225     QElementDef def = getElement(element.getNodeName());
226     if (def != null)
227       def.fillDefaults(element);
228   }
229
230   void print(XmlPrinter os) throws IOException JavaDoc
231   {
232     if (getName() == null)
233       return;
234
235     os.printHeader(getName());
236     
237     os.print("<!DOCTYPE ");
238     os.print(getName());
239     
240     if (_publicId != null) {
241       os.print(" PUBLIC \"");
242       os.print(_publicId);
243       os.print("\" \"");
244       os.print(_systemId);
245       os.print("\"");
246     } else if (_systemId != null) {
247       os.print(" SYSTEM \"");
248       os.print(_systemId);
249       os.print("\"");
250     }
251
252     if (_firstChild != null) {
253       os.println(" [");
254
255       for (QAbstractNode node = _firstChild; node != null; node = node._next) {
256     node.print(os);
257       }
258
259       os.println("]>");
260     } else
261       os.println(">");
262   }
263
264   public String JavaDoc toString()
265   {
266     return "QDocumentType[" + _name + "]";
267   }
268 }
269
Popular Tags