1 23 24 package org.enhydra.xml.xmlc.dom; 25 26 import java.util.TreeMap ; 27 import java.util.TreeSet ; 28 29 import org.enhydra.xml.io.DOMFormatter; 30 import org.enhydra.xml.io.Encodings; 31 import org.enhydra.xml.xmlc.XMLCException; 32 import org.w3c.dom.Document ; 33 import org.w3c.dom.DocumentType ; 34 import org.w3c.dom.Element ; 35 import org.w3c.dom.Node ; 36 import org.w3c.dom.html.HTMLDocument; 37 38 49 50 51 56 final public class XMLCDocument { 57 60 private XMLCDomFactory fDomFactory; 61 62 65 private boolean fIsHtmlDocument; 66 67 70 private boolean fIsHtmlFrameSet; 71 72 75 private String fXMLVersion; 76 private String fEncoding; 77 78 81 private Document fDocument; 82 83 86 private DocumentType fDocType; 87 88 91 private TreeMap fElementIdAttrs = new TreeMap (); 92 93 96 private TreeSet fIdAttrNames = new TreeSet (); 97 98 102 private String fLastIdAttrName; 103 104 107 private TreeSet fPCDataElements = new TreeSet (); 108 109 117 public XMLCDocument(XMLCDomFactory domFactory) throws XMLCException { 118 fDomFactory = domFactory; 119 } 120 121 124 public XMLCDomFactory getDomFactory() { 125 return fDomFactory; 126 } 127 128 131 public DocumentType createDocumentType(String qualifiedName, 132 String publicID, 133 String systemID, 134 String internalSubset) { 135 fDocType = fDomFactory.createDocumentType(qualifiedName, 136 publicID, systemID, 137 internalSubset); 138 return fDocType; 139 } 140 141 144 public Document createDocument(String namespaceURI, 145 String qualifiedName) { 146 fDocument = fDomFactory.createDocument(namespaceURI, qualifiedName, 147 fDocType); 148 fIsHtmlDocument = (fDocument instanceof HTMLDocument); 149 return fDocument; 150 } 151 152 155 public Document getDocument() { 156 return fDocument; 157 } 158 159 162 public boolean isHtmlDocument() { 163 return fIsHtmlDocument; 164 } 165 166 169 public boolean isHtmlFrameSet() { 170 return fIsHtmlFrameSet; 171 } 172 173 176 public void setIsHtmlFrameSet() { 177 fIsHtmlFrameSet = true; 178 } 179 180 185 public void setXMLVersion(String xmlVersion) { 186 fXMLVersion = xmlVersion; 187 } 188 189 192 public String getXMLVersion() { 193 return fXMLVersion; 194 } 195 196 199 public String getEncoding() { 200 return fEncoding; 201 } 202 203 207 public void setEncoding(String encoding) { 208 if (encoding == null) { 209 fEncoding = null; 210 } else { 211 Encodings encodings = Encodings.getEncodings(); 212 String mimeEncoding = encodings.getMIMEPreferred(encoding); 213 if (mimeEncoding != null) { 214 fEncoding = mimeEncoding; 215 } else { 216 fEncoding = encoding; 217 } 218 } 219 } 220 221 226 public void addPCDataContentElement(String elementName) { 227 fPCDataElements.add(elementName); 228 } 229 230 238 public void addIdAttribute(String elementName, 239 String attributeName) { 240 fElementIdAttrs.put(elementName, attributeName); 241 if (!(attributeName.equals(fLastIdAttrName) 242 || fIdAttrNames.contains(attributeName))) { 243 fIdAttrNames.add(attributeName); 244 fLastIdAttrName = attributeName; 245 } 246 } 247 248 254 public String nodeClassToInterface(Node node) { 255 return fDomFactory.nodeClassToInterface(node); 256 } 257 258 275 public String getGlobalIdAttribute() { 276 if (fIsHtmlDocument) { 277 return "id"; 278 } else { 279 if (fIdAttrNames.size() == 1) { 280 return fLastIdAttrName; 281 } else { 282 return null; 283 } 284 } 285 } 286 287 295 public String getIdAttrName(Element element) { 296 if (fIsHtmlDocument) { 297 return "id"; 298 } else { 299 return (String )fElementIdAttrs.get(element.getTagName()); 300 } 301 } 302 303 309 public String getElementId(Element element) { 310 String idAttrName = getIdAttrName(element); 311 if (idAttrName == null) { 312 return null; } 314 String id = element.getAttribute(idAttrName); 315 if ((id == null) || (id.length() == 0)) { 316 return null; } 318 return id; 319 } 320 321 325 public String [] getElementClassNames(Element element) { 326 return fDomFactory.getElementClassNames(element); 327 } 328 329 337 public String getElementName(Element element) { 338 if (fIsHtmlDocument) { 339 String name = element.getAttribute("name"); 340 if (name.length() == 0) { 341 return null; 342 } else { 343 return name; 344 } 345 } else { 346 return null; 347 } 348 } 349 350 354 public boolean isURLAttribute(Element element, 355 String attrName) { 356 return fDomFactory.isURLAttribute(element, attrName); 357 } 358 359 362 public boolean hasPCDataInContentModel(Element element) { 363 return fPCDataElements.contains(element.getTagName()); 364 } 365 366 370 public String toDocument() { 371 return new DOMFormatter(DOMFormatter.getDefaultOutputOptions(getDocument())).toString(getDocument()); 372 } 373 } 374 | Popular Tags |