org.mmbase.util.xml
Class DocumentWriter

java.lang.Object
  extended byorg.mmbase.util.xml.DocumentReader
      extended byorg.mmbase.util.xml.DocumentWriter
Direct Known Subclasses:
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,v 1.4 2003/08/18 16:50:53 pierre Exp $
Author:
Pierre van Rooden

Field Summary
 
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
 
Constructor Summary
DocumentWriter(org.xml.sax.InputSource source)
          Constructs the document by reading it from a source.
DocumentWriter(org.xml.sax.InputSource source, boolean validating, java.lang.Class resolveBase)
          Constructs the document by reading it from a source.
DocumentWriter(java.lang.String path)
          Constructs the document by reading it from a file.
DocumentWriter(java.lang.String path, boolean validating, java.lang.Class resolveBase)
          Constructs the document by reading it from a source.
DocumentWriter(java.lang.String qualifiedName, java.lang.String publicId, java.lang.String systemId)
          Constructs the document writer.
 
Method Summary
protected  org.w3c.dom.Comment addComment(java.lang.String key, org.w3c.dom.Element out)
          Creates a Comment (provided comments should be included), and adds it to the specified node as a child.
protected  org.w3c.dom.Comment addComment(java.lang.String key, java.lang.String a1, org.w3c.dom.Element out)
          Creates a Comment (provided comments should be included), and adds it to the specified node as a child.
protected  org.w3c.dom.Comment addComment(java.lang.String key, java.lang.String a1, java.lang.String a2, org.w3c.dom.Element out)
          Creates a Comment (provided comments should be included), and adds it to the specified node as a child.
protected  org.w3c.dom.Element addContentElement(java.lang.String tagname, java.lang.String content, org.w3c.dom.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.
 org.w3c.dom.Document generateDocument()
          Generates the document if it hadn't be done so already.
 org.w3c.dom.Document getDocument()
          Returns the completed document representation; If the document was not yet generated, it is generated by calling generateDocument().
 boolean getIncludeComments()
          Gets whether the document will include comments
protected  java.lang.String getMessage(java.lang.String key)
          Retrieves a message from the resource bundle.
protected  java.lang.String getMessage(java.lang.String key, java.lang.String a1)
          Retrieves a message from the resource bundle.
protected  java.lang.String getMessage(java.lang.String key, java.lang.String a1, java.lang.String a2)
          Retrieves a message from the resource bundle.
protected  java.lang.String getMessage(java.lang.String key, java.lang.String a1, java.lang.String a2, java.lang.String a3)
          Retrieves a message from the resource bundle.
protected  void getMessageRetriever(java.lang.String resourcelocation)
          Initialize the ResourceBundle with the given resource.
 void setIncludeComments(boolean value)
          Sets whether the document will include comments
 void write(javax.xml.transform.stream.StreamResult result)
          Generates the document and writes it to the result object.
 void writeToFile(java.lang.String filename)
          Generates the document and store it as a file in the given path.
 void writeToStream(java.io.OutputStream out)
          Generates the document and store it in the given stream.
 java.lang.String writeToString()
          Generates the document and returns it as a string.
 
Methods inherited from class org.mmbase.util.xml.DocumentReader
getDocumentBuilder, getDocumentBuilder, getFileName, getNodeTextValue, registerPublicIDs, validate
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DocumentWriter

public DocumentWriter(java.lang.String qualifiedName,
                      java.lang.String publicId,
                      java.lang.String systemId)
               throws org.w3c.dom.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

DocumentWriter

public DocumentWriter(java.lang.String path)
Constructs the document by reading it from a file.

Parameters:
path - the path to the file from which to read the document

DocumentWriter

public DocumentWriter(java.lang.String path,
                      boolean validating,
                      java.lang.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:
path - the path to the file 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

DocumentWriter

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

Parameters:
source - the input source from which to read the document

DocumentWriter

public DocumentWriter(org.xml.sax.InputSource source,
                      boolean validating,
                      java.lang.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
Method Detail

getMessageRetriever

protected void getMessageRetriever(java.lang.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 java.lang.String getMessage(java.lang.String key)
Retrieves a message from the resource bundle.

Parameters:
key - the key of the message

getMessage

protected java.lang.String getMessage(java.lang.String key,
                                      java.lang.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 java.lang.String getMessage(java.lang.String key,
                                      java.lang.String a1,
                                      java.lang.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 java.lang.String getMessage(java.lang.String key,
                                      java.lang.String a1,
                                      java.lang.String a2,
                                      java.lang.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 org.w3c.dom.Element addContentElement(java.lang.String tagname,
                                                java.lang.String content,
                                                org.w3c.dom.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 org.w3c.dom.Comment addComment(java.lang.String key,
                                         org.w3c.dom.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 org.w3c.dom.Comment addComment(java.lang.String key,
                                         java.lang.String a1,
                                         org.w3c.dom.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 org.w3c.dom.Comment addComment(java.lang.String key,
                                         java.lang.String a1,
                                         java.lang.String a2,
                                         org.w3c.dom.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 org.w3c.dom.DOMException
Generates the document. You need to override this class with the code that constructs your document.

Throws:
org.w3c.dom.DOMException - when an error occurred during generation

generateDocument

public final org.w3c.dom.Document generateDocument()
                                            throws org.w3c.dom.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:
org.w3c.dom.DOMException - when an error occurred during generation
org.w3c.dom.DOMException - when the document was already constructed

getDocument

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

Returns:
the generated document
Throws:
org.w3c.dom.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

getIncludeComments

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

Returns:
if true, the document will include comments

writeToString

public java.lang.String writeToString()
                               throws java.io.IOException,
                                      javax.xml.transform.TransformerException
Generates the document and returns it as a string.

Throws:
java.io.IOException
javax.xml.transform.TransformerException

writeToFile

public void writeToFile(java.lang.String filename)
                 throws java.io.IOException,
                        javax.xml.transform.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:
java.io.IOException
javax.xml.transform.TransformerException

writeToStream

public void writeToStream(java.io.OutputStream out)
                   throws java.io.IOException,
                          javax.xml.transform.TransformerException
Generates the document and store it in the given stream.

Parameters:
out - the output stream where the configuration is to be stored
Throws:
java.io.IOException
javax.xml.transform.TransformerException

write

public void write(javax.xml.transform.stream.StreamResult result)
           throws java.io.IOException,
                  javax.xml.transform.TransformerException
Generates the document and writes it to the result object.

Parameters:
result - the StreamResult object where to store the configuration'
Throws:
java.io.IOException
javax.xml.transform.TransformerException


MMBase build 1.7.1.20041002