| 1 10 package org.mmbase.util.xml; 11 12 import java.io.*; 13 import java.util.ResourceBundle ; 14 import java.util.MissingResourceException ; 15 16 import org.xml.sax.InputSource ; 17 18 import org.w3c.dom.*; 19 import javax.xml.transform.*; 20 import javax.xml.transform.stream.StreamResult ; 21 import javax.xml.transform.dom.DOMSource ; 22 23 import org.mmbase.util.logging.*; 24 25 35 abstract public class DocumentWriter extends DocumentReader { 36 37 private static final Logger log = Logging.getLoggerInstance(DocumentWriter.class); 39 40 43 private boolean documentGenerated=false; 44 45 48 private boolean includeComments = false; 49 50 53 private ResourceBundle messageRB; 54 55 String publicId =""; 57 String systemId =""; 58 59 69 public DocumentWriter(String qualifiedName, String publicId, String systemId) throws DOMException { 70 DOMImplementation domImpl = DocumentReader.getDocumentBuilder().getDOMImplementation(); 71 this.publicId = publicId; 72 this.systemId = systemId; 73 DocumentType doctype = domImpl.createDocumentType(qualifiedName, this.publicId, this.systemId); 74 document = domImpl.createDocument(null, qualifiedName, doctype); 75 } 76 77 78 83 public DocumentWriter(InputSource source) { 84 super(source); 85 documentGenerated = true; 86 } 87 88 97 public DocumentWriter(InputSource source, boolean validating, Class resolveBase) { 98 super(source, validating, resolveBase); 99 documentGenerated = true; 100 } 101 102 107 protected void getMessageRetriever(String resourcelocation) { 108 try { 109 messageRB = ResourceBundle.getBundle(resourcelocation); 110 } catch (MissingResourceException e) { 111 log.error("Resource for DocumentWriter is missing: "+resourcelocation); 112 } 113 } 114 115 119 protected String getMessage(String key) { 120 return getMessage(key,""); 121 } 122 123 128 protected String getMessage(String key, String a1) { 129 return getMessage(key,a1,"",""); 130 } 131 132 138 protected String getMessage(String key, String a1, String a2) { 139 return getMessage(key,a1,a2,""); 140 } 141 142 149 protected String getMessage(String key, String a1, String a2, String a3) { 150 if (messageRB!=null) 151 try { 152 String message = messageRB.getString(key); 153 Object [] args = new String [3]; 154 args[0] = a1; 155 args[1] = a2; 156 args[2] = a3; 157 return java.text.MessageFormat.format(message, args); 158 } catch (MissingResourceException e) { 159 log.error("Resource for DocumentWriter is broken. There is no " + key + " key in resource."); 160 } 161 return null; 162 } 163 164 172 protected Element addContentElement(String tagname,String content, Element out) { 173 Element el = document.createElement(tagname); 174 if (content == null) content=""; 175 Text tel = document.createTextNode(content); 176 el.appendChild(tel); 177 out.appendChild(el); 178 return el; 179 } 180 181 191 protected Comment addComment(String key, Element out) { 192 return addComment(key, "", "", out); 193 } 194 195 206 protected Comment addComment(String key, String a1, Element out) { 207 return addComment(key, a1, "", out); 208 } 209 210 222 protected Comment addComment(String key, String a1, String a2, Element out) { 223 Comment comm=null; 224 if (includeComments) { 225 String message=getMessage(key,a1,a2); 226 if (message!=null) { 227 comm=document.createComment(" "+message+" "); 228 out.appendChild(comm); 229 } 230 } 231 return comm; 232 } 233 234 239 abstract protected void generate() throws DOMException; 240 241 248 public final Document generateDocument() throws DOMException { 249 if (!documentGenerated) { 250 generate(); 251 documentGenerated=true; 252 return document; 253 } else { 254 throw new IllegalStateException ("Document already constructed"); 255 } 256 } 257 258 264 public Document getDocument() throws DOMException { 265 if (!documentGenerated) { 266 generateDocument(); 267 } 268 return document; 269 } 270 271 275 public void setIncludeComments(boolean value) { 276 includeComments=value; 277 } 278 279 283 public boolean includeComments() { 284 return includeComments; 285 } 286 287 291 public String writeToString() throws TransformerException { 292 StringWriter strw=new StringWriter(500); 293 write(new StreamResult (strw)); 294 return strw.toString(); 295 } 296 297 303 public void writeToFile(String filename) throws IOException, TransformerException { 304 writeToStream(new FileOutputStream(filename)); 305 } 306 307 311 public void writeToStream(OutputStream out) throws TransformerException { 312 write(new StreamResult (out)); 313 } 314 315 319 public void write(StreamResult result) throws TransformerException { 320 Document doc=getDocument(); 321 TransformerFactory tfactory = TransformerFactory.newInstance(); 322 tfactory.setURIResolver(new org.mmbase.util.xml.URIResolver(new java.io.File (""))); 323 Transformer serializer = tfactory.newTransformer(); 326 serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); 329 serializer.setOutputProperty(OutputKeys.INDENT, "yes"); 331 serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no"); 332 serializer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, publicId); 333 serializer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemId); 334 serializer.transform(new DOMSource (doc), result); 335 } 336 } 337 | Popular Tags |