001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.xml;
031:
032: import org.w3c.dom.DocumentType;
033: import org.w3c.dom.NamedNodeMap;
034: import org.w3c.dom.Node;
035:
036: import java.io.IOException;
037: import java.util.HashMap;
038: import java.util.Iterator;
039:
040: public class QDocumentType extends QNode implements DocumentType {
041: String _name;
042: HashMap<String, QElementDef> _elements = new HashMap<String, QElementDef>();
043: HashMap<String, QEntity> _entities = new HashMap<String, QEntity>();
044: HashMap<String, QNotation> _notations = new HashMap<String, QNotation>();
045: HashMap<String, QEntity> _parameterEntities = new HashMap<String, QEntity>();
046: HashMap<String, String> _ids = new HashMap<String, String>();
047: String _systemId;
048: String _publicId;
049:
050: boolean _hasAttributeDefaults;
051:
052: /**
053: * Create a new document type.
054: */
055: public QDocumentType(String name) {
056: this (name, null, null);
057: }
058:
059: public QDocumentType(String name, String publicId, String systemId) {
060: _name = name;
061:
062: _entities.put("amp", new QEntity("amp", "&"));
063: _entities.put("lt", new QEntity("lt", "<"));
064: _entities.put("gt", new QEntity("gt", ">"));
065: _entities.put("quot", new QEntity("quot", "\""));
066: _entities.put("apos", new QEntity("apos", "'"));
067:
068: _publicId = publicId;
069: _systemId = systemId;
070: }
071:
072: public String getNodeName() {
073: return _name;
074: }
075:
076: public String getTagName() {
077: return "#documenttype";
078: }
079:
080: public short getNodeType() {
081: return DOCUMENT_TYPE_NODE;
082: }
083:
084: public String getPrefix() {
085: return null;
086: }
087:
088: public String getLocalName() {
089: return null;
090: }
091:
092: public String getNamespaceURI() {
093: return null;
094: }
095:
096: public String getName() {
097: return _name;
098: }
099:
100: public void setName(String name) {
101: _name = name;
102: }
103:
104: public NamedNodeMap getEntities() {
105: return new QNamedNodeMap(_entities);
106: }
107:
108: public NamedNodeMap getNotations() {
109: return new QNamedNodeMap(_notations);
110: }
111:
112: public void setLocation(String filename, int line, int col) {
113: }
114:
115: Node importNode(QDocument owner, boolean deep) {
116: QDocumentType ref = new QDocumentType(_name);
117:
118: return ref;
119: }
120:
121: void addNotation(QNotation notation) {
122: _notations.put(notation._name, notation);
123: }
124:
125: public String getElementId(String element) {
126: return _ids.get(element);
127: }
128:
129: public Iterator getElementIdNames() {
130: return _ids.keySet().iterator();
131: }
132:
133: void setElementId(String element, String id) {
134: _ids.put(element, id);
135: }
136:
137: /**
138: * Adds a new defined entity.
139: */
140: void addEntity(QEntity entity) {
141: if (_entities.get(entity._name) == null)
142: _entities.put(entity._name, entity);
143: }
144:
145: QEntity getEntity(String name) {
146: return _entities.get(name);
147: }
148:
149: void addParameterEntity(QEntity entity) {
150: if (_parameterEntities.get(entity._name) == null)
151: _parameterEntities.put(entity._name, entity);
152: }
153:
154: QEntity getParameterEntity(String name) {
155: return _parameterEntities.get(name);
156: }
157:
158: String getEntityValue(String name) {
159: QEntity entity = _entities.get(name);
160:
161: if (entity == null)
162: return null;
163: else
164: return entity._value;
165: }
166:
167: public String getSystemId() {
168: return _systemId;
169: }
170:
171: protected void setSystemId(String systemId) {
172: _systemId = systemId;
173: }
174:
175: public String getPublicId() {
176: return _publicId;
177: }
178:
179: protected void setPublicId(String publicId) {
180: _publicId = publicId;
181: }
182:
183: public String getInternalSubset() {
184: return null;
185: }
186:
187: boolean isExternal() {
188: return _systemId != null || _publicId != null;
189: }
190:
191: public QElementDef getElement(String name) {
192: return _elements.get(name);
193: }
194:
195: QElementDef addElement(String name) {
196: QElementDef def = _elements.get(name);
197: if (def == null) {
198: def = new QElementDef(name);
199: def._dtd = this ;
200: def._owner = _owner;
201: _elements.put(name, def);
202: appendChild(def);
203: }
204:
205: return def;
206: }
207:
208: void setAttributeDefaults() {
209: _hasAttributeDefaults = true;
210: }
211:
212: boolean hasAttributeDefaults() {
213: return _hasAttributeDefaults;
214: }
215:
216: void fillDefaults(QElement element) {
217: if (!_hasAttributeDefaults)
218: return;
219:
220: QElementDef def = getElement(element.getNodeName());
221: if (def != null)
222: def.fillDefaults(element);
223: }
224:
225: void print(XmlPrinter os) throws IOException {
226: if (getName() == null)
227: return;
228:
229: os.printHeader(getName());
230:
231: os.print("<!DOCTYPE ");
232: os.print(getName());
233:
234: if (_publicId != null) {
235: os.print(" PUBLIC \"");
236: os.print(_publicId);
237: os.print("\" \"");
238: os.print(_systemId);
239: os.print("\"");
240: } else if (_systemId != null) {
241: os.print(" SYSTEM \"");
242: os.print(_systemId);
243: os.print("\"");
244: }
245:
246: if (_firstChild != null) {
247: os.println(" [");
248:
249: for (QAbstractNode node = _firstChild; node != null; node = node._next) {
250: node.print(os);
251: }
252:
253: os.println("]>");
254: } else
255: os.println(">");
256: }
257:
258: public String toString() {
259: return "QDocumentType[" + _name + "]";
260: }
261: }
|