1 29 30 package com.caucho.xml; 31 32 import org.w3c.dom.DocumentType ; 33 import org.w3c.dom.NamedNodeMap ; 34 import org.w3c.dom.Node ; 35 36 import java.io.IOException ; 37 import java.util.HashMap ; 38 import java.util.Iterator ; 39 40 public class QDocumentType extends QNode implements DocumentType { 41 String _name; 42 HashMap <String ,QElementDef> _elements = new HashMap <String ,QElementDef>(); 43 HashMap <String ,QEntity> _entities = new HashMap <String ,QEntity>(); 44 HashMap <String ,QNotation> _notations = new HashMap <String ,QNotation>(); 45 HashMap <String ,QEntity> _parameterEntities = new HashMap <String ,QEntity>(); 46 HashMap <String ,String > _ids = new HashMap <String ,String >(); 47 String _systemId; 48 String _publicId; 49 50 boolean _hasAttributeDefaults; 51 52 55 public QDocumentType(String name) 56 { 57 this(name, null, null); 58 } 59 60 public QDocumentType(String name, String publicId, String 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 getNodeName() { return _name; } 75 public String getTagName() { return "#documenttype"; } 76 public short getNodeType() { return DOCUMENT_TYPE_NODE; } 77 78 public String getPrefix() { return null; } 79 public String getLocalName() { return null; } 80 public String getNamespaceURI() { return null; } 81 82 public String getName() { return _name; } 83 public void setName(String name) { _name = name; } 84 85 public NamedNodeMap getEntities() 86 { 87 return new QNamedNodeMap(_entities); 88 } 89 90 public NamedNodeMap getNotations() 91 { 92 return new QNamedNodeMap(_notations); 93 } 94 95 public void setLocation(String filename, int line, int col) 96 { 97 } 98 99 Node 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 getElementId(String element) 112 { 113 return _ids.get(element); 114 } 115 116 public Iterator getElementIdNames() 117 { 118 return _ids.keySet().iterator(); 119 } 120 121 void setElementId(String element, String id) 122 { 123 _ids.put(element, id); 124 } 125 126 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 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 name) 147 { 148 return _parameterEntities.get(name); 149 } 150 151 String getEntityValue(String 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 getSystemId() 162 { 163 return _systemId; 164 } 165 166 protected void setSystemId(String systemId) 167 { 168 _systemId = systemId; 169 } 170 171 public String getPublicId() 172 { 173 return _publicId; 174 } 175 176 protected void setPublicId(String publicId) 177 { 178 _publicId = publicId; 179 } 180 181 public String getInternalSubset() 182 { 183 return null; 184 } 185 186 boolean isExternal() 187 { 188 return _systemId != null || _publicId != null; 189 } 190 191 public QElementDef getElement(String name) 192 { 193 return _elements.get(name); 194 } 195 196 QElementDef addElement(String 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 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 toString() 265 { 266 return "QDocumentType[" + _name + "]"; 267 } 268 } 269 | Popular Tags |