org.mmbase.util.xml
Class DocumentWriter

java.lang.Object
  extended by org.mmbase.util.xml.DocumentReader
      extended by org.mmbase.util.xml.DocumentWriter
Direct Known Subclasses:
ApplicationWriter, BuilderWriter, ModuleWriter

public abstract class DocumentWriter
extends DocumentReader

Abstract class for creating xml documents. Use this class as the base class for writers that construct and export DOM documents. The document can then be used internally or serialized using a number of utility methods.

Since:
MMBase-1.6
Version:
$Id: DocumentWriter.java 41059 2010-02-16 00:15:41Z michiel $
Author:
Pierre van Rooden

Field Summary
(package private)  String publicId
           
(package private)  String systemId
           
 
Fields inherited from class org.mmbase.util.xml.DocumentReader
document, DTD_ERROR, DTD_ERROR_1_0, FILENOTFOUND, PUBLIC_ID_ERROR, PUBLIC_ID_ERROR_1_0, utilProperties
 
Constructor Summary
DocumentWriter(InputSource source)
          Constructs the document by reading it from a source.
DocumentWriter(InputSource source, boolean validating, Class<?> resolveBase)
          Constructs the document by reading it from a source.
DocumentWriter(String qualifiedName, String publicId, String systemId)
          Constructs the document writer.
DocumentWriter(String qualifiedName, String publicId, String systemId, boolean schema)
          Constructs the document writer.
 
Method Summary
protected  Comment addComment(String key, Element out)
          Creates a Comment (provided comments should be included), and adds it to the specified node as a child.
protected  Comment addComment(String key, String a1, Element out)
          Creates a Comment (provided comments should be included), and adds it to the specified node as a child.
protected  Comment addComment(String key, String a1, String a2, Element out)
          Creates a Comment (provided comments should be included), and adds it to the specified node as a child.
protected  Element addContentElement(String tagname, String content, Element out)
          Creates a DOM element which contains a Text Node, and adds it to the specified node as a child.
protected abstract  void generate()
          Generates the document.
 Document generateDocument()
          Generates the document if it hadn't be done so already.
 Document getDocument()
          Returns the completed document representation; If the document was not yet generated, it is generated by calling generateDocument().
protected  String getMessage(String key)
          Retrieves a message from the resource bundle.
protected  String getMessage(String key, String a1)
          Retrieves a message from the resource bundle.
protected  String getMessage(String key, String a1, String a2)
          Retrieves a message from the resource bundle.
protected  String getMessage(String key, String a1, String a2, String a3)
          Retrieves a message from the resource bundle.
protected  void getMessageRetriever(String resourcelocation)
          Initialize the ResourceBundle with the given resource.
 boolean includeComments()
          Gets whether the document will include comments
 void setIncludeComments(boolean value)
          Sets whether the document will include comments
 void write(StreamResult result)
          Generates the document and writes it to the result object.
 void writeToFile(String filename)
          Generates the document and store it as a file in the given path.
 void writeToStream(OutputStream out)
          Generates the document and store it in the given stream.
 String writeToString()
          Generates the document and returns it as a string.
 
Methods inherited from class org.mmbase.util.xml.DocumentReader
appendChild, getAttribute, getChildElements, getChildElements, getChildElements, getChildElements, getDocumentBuilder, getDocumentBuilder, getDocumentBuilder, getDocumentBuilder, getElementAttributeValue, getElementAttributeValue, getElementByPath, getElementByPath, getElementName, getElementValue, getElementValue, getNodeTextValue, getNodeTextValue, getRootElement, getSystemId, hasAttribute, main, registerPublicIDs, setNodeTextValue, setPrefix, setSystemId, toDocument, validate, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

publicId

String publicId

systemId

String systemId
Constructor Detail

DocumentWriter

public DocumentWriter(String qualifiedName,
                      String publicId,
                      String systemId)
               throws DOMException
Constructs the document writer. The constructor creates a basic document with a root element based on the specified document type parameters. The document is empty after construction. It is actually filled with a call to generateDocument(), which is in turn called when the document is first accessed through getDocument().

Parameters:
qualifiedName - the qualified name of the document's root element
publicId - the PUBLIC id of the document type
systemId - the SYSTEm id of the document type
Throws:
DOMException

DocumentWriter

public DocumentWriter(String qualifiedName,
                      String publicId,
                      String systemId,
                      boolean schema)
               throws DOMException
Constructs the document writer. The constructor creates a basic document with a root element based on the specified document type parameters. The document is empty after construction. It is actually filled with a call to generateDocument(), which is in turn called when the document is first accessed through getDocument().

Parameters:
qualifiedName - the qualified name of the document's root element
publicId - the PUBLIC id of the document type
systemId - the SYSTEm id of the document type
schema - should the publicId and systemId interpreted as XSD namespace and location
Throws:
DOMException

DocumentWriter

public DocumentWriter(InputSource source)
Constructs the document by reading it from a source.

Parameters:
source - the input source from which to read the document
Since:
MMBase-1.7

DocumentWriter

public DocumentWriter(InputSource source,
                      boolean validating,
                      Class<?> resolveBase)
Constructs the document by reading it from a source. You can pass a resolve class to this constructor, allowing you to indicate the package in which the dtd of the document read is to be found. The dtd sould be in the resources package under the package of the class passed.

Parameters:
source - the input source from which to read the document
validating - whether to validate the document
resolveBase - the base class whose package is used to resolve dtds, set to null if unknown
Since:
MMBase-1.7
Method Detail

getMessageRetriever

protected void getMessageRetriever(String resourcelocation)
Initialize the ResourceBundle with the given resource. You need a respource to use the addfCOmment() and getMessage() methods.

Parameters:
resourcelocation - Resource.

getMessage

protected String getMessage(String key)
Retrieves a message from the resource bundle.

Parameters:
key - the key of the message

getMessage

protected String getMessage(String key,
                            String a1)
Retrieves a message from the resource bundle.

Parameters:
key - the key of the message
a1 - the first parameter to substitute in the message

getMessage

protected String getMessage(String key,
                            String a1,
                            String a2)
Retrieves a message from the resource bundle.

Parameters:
key - the key of the message
a1 - the first parameter to substitute in the message
a2 - the second parameter to substitute in the message

getMessage

protected String getMessage(String key,
                            String a1,
                            String a2,
                            String a3)
Retrieves a message from the resource bundle.

Parameters:
key - the key of the message
a1 - the first parameter to substitute in the message
a2 - the second parameter to substitute in the message
a3 - the third parameter to substitute in the message

addContentElement

protected Element addContentElement(String tagname,
                                    String content,
                                    Element out)
Creates a DOM element which contains a Text Node, and adds it to the specified node as a child.

Parameters:
tagname - name of the new element
content - content of the new element as a string
out - the element to which to add the new Element.
Returns:
the newly created element

addComment

protected Comment addComment(String key,
                             Element out)
Creates a Comment (provided comments should be included), and adds it to the specified node as a child. The comment is retrieved from a resource bundle - if no resource was specified, no comments are added.

Parameters:
key - the key of the comment to add as a string
out - the element to which to add the new Comment.
Returns:
the newly created comment or null if nothing was added
See Also:
setIncludeComments(boolean)

addComment

protected Comment addComment(String key,
                             String a1,
                             Element out)
Creates a Comment (provided comments should be included), and adds it to the specified node as a child. The comment is retrieved from a resource bundle - if no resource was specified, no comments are added.

Parameters:
key - the key of the comment to add as a string
a1 - the first parameter to substitute in the comment
out - the element to which to add the new Comment.
Returns:
the newly created comment or null if nothing was added
See Also:
setIncludeComments(boolean)

addComment

protected Comment addComment(String key,
                             String a1,
                             String a2,
                             Element out)
Creates a Comment (provided comments should be included), and adds it to the specified node as a child. The comment is retrieved from a resource bundle - if no resource was specified, no comments are added.

Parameters:
key - the comment to add as a string
a1 - the first parameter to substitute in the comment
a2 - the second parameter to substitute in the comment
out - the element to which to add the new Comment.
Returns:
the newly created comment or null if nothing was added
See Also:
setIncludeComments(boolean)

generate

protected abstract void generate()
                          throws DOMException
Generates the document. You need to override this class with the code that constructs your document.

Throws:
DOMException - when an error occurred during generation

generateDocument

public final Document generateDocument()
                                throws DOMException
Generates the document if it hadn't be done so already. If not, an exception is thrown. Use getDocument() to safely retrive a generated coeumnt.

Throws:
DOMException - when an error occurred during generation
DOMException - when the document was already constructed

getDocument

public Document getDocument()
                     throws DOMException
Returns the completed document representation; If the document was not yet generated, it is generated by calling generateDocument().

Overrides:
getDocument in class DocumentReader
Returns:
the generated document
Throws:
DOMException - when an error occurred during generation

setIncludeComments

public void setIncludeComments(boolean value)
Sets whether the document will include comments

Parameters:
value - if true, the document will include comments

includeComments

public boolean includeComments()
Gets whether the document will include comments

Returns:
if true, the document will include comments

writeToString

public String writeToString()
                     throws TransformerException
Generates the document and returns it as a string.

Throws:
TransformerException - if the document is malformed

writeToFile

public void writeToFile(String filename)
                 throws IOException,
                        TransformerException
Generates the document and store it as a file in the given path.

Parameters:
filename - the filepath where the configuration is to be stored
Throws:
TransformerException - if the document is malformed
IOException - if the file cannot be written

writeToStream

public void writeToStream(OutputStream out)
                   throws TransformerException
Generates the document and store it in the given stream.

Parameters:
out - the output stream where the configuration is to be stored
Throws:
TransformerException

write

public void write(StreamResult result)
           throws TransformerException
Generates the document and writes it to the result object.

Parameters:
result - the StreamResult object where to store the configuration'
Throws:
TransformerException


MMBase 2.0-SNAPSHOT - null