org.apache.jackrabbit.xml
Class DocumentViewExportVisitor

java.lang.Object
  extended byorg.apache.jackrabbit.xml.DocumentViewExportVisitor
All Implemented Interfaces:
ItemVisitor

public class DocumentViewExportVisitor
extends Object
implements ItemVisitor

Generic document view exporter for JCR content repositories. This class can be used to implement the XML document view export operations using nothing but the standard JCR interfaces. The export operation is implemented as an ItemVisitor that generates the document view SAX event stream as it traverses the selected JCR content tree.

Implementing a customized XML serializer

A client can extend this class to provide customized XML serialization formats. By overriding the protected includeProperty() and includeNode() methods, a subclass can select which properties and nodes will be included in the serialized XML stream.

For example, the following code implements an XML serialization that only contains the titles of the first two levels of the node tree.

     ContentHandler handler = ...;
     Node parent = ...;
     parent.accept(
         new DocumentViewExportVisitor(handler, true, false) {

             protected boolean includeProperty(Property property)
                     throws RepositoryException {
                 return property.getName().equals("title");
             }

             protected boolean includeNode(Node node)
                     throws RepositoryException {
                 return (node.getDepth() <= root.getDepth() + 2);
             }

         });
 

Implementing the standard export methods

The following is an example of the Session.exportDocumentView(String, ContentHandler, boolean, boolean) method implemented in terms of this exporter class:

     public void exportDocumentView(
             String absPath, ContentHandler handler,
             boolean skipBinary, boolean noRecurse)
             throws PathNotFoundException, SAXException, RepositoryException {
         Item item = getItem(absPath);
         if (item.isNode()) {
             item.accept(new DocumentViewExportVisitor(
                     handler, skipBinary, noRecurse));
         } else {
             throw new PathNotFoundException("Invalid node path: " + path);
         }
     }
 

The companion method Session.exportDocumentView(String, OutputStream, boolean, boolean) can be implemented in terms of the above method and the XMLSerializer class from the Xerces library:

 import org.apache.xml.serialize.XMLSerializer;
 import org.apache.xml.serialize.OutputFormat;

     public void exportDocumentView(
             String absPath, OutputStream output,
             boolean skipBinary, boolean noRecurse)
             throws PathNotFoundException, IOException, RepositoryException {
         try {
             XMLSerializer serializer =
                 new XMLSerializer(output, new OutputFormat());
             exportDocView(
                     absPath, serializer.asContentHandler(),
                     binaryAsLink, noRecurse);
         } catch (SAXException e) {
             throw new IOException(e.getMessage());
         }
     }
 

See Also:
ItemVisitor, Session.exportDocumentView(String, ContentHandler, boolean, boolean), Session.exportDocumentView(String, java.io.OutputStream, boolean, boolean)

Field Summary
protected  Node root
          The root node of the serialization tree.
 
Constructor Summary
DocumentViewExportVisitor(ContentHandler handler, boolean skipBinary, boolean noRecurse)
          Creates an visitor for exporting content using the document view format.
 
Method Summary
protected  boolean includeNode(Node node)
          Checks whether the given node should be included in the XML serialization.
protected  boolean includeProperty(Property property)
          Checks whether the given property should be included in the XML serialization.
 void visit(Node node)
          Exports the visited node using the document view serialization format.
 void visit(Property property)
          Ignored.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

root

protected Node root
The root node of the serialization tree. This is the node that is mapped to the root element of the serialized XML stream.

Constructor Detail

DocumentViewExportVisitor

public DocumentViewExportVisitor(ContentHandler handler,
                                 boolean skipBinary,
                                 boolean noRecurse)
Creates an visitor for exporting content using the document view format. To actually perform the export operation, you need to pass the visitor instance to the selected content node using the Node.accept(ItemVisitor) method.

Parameters:
handler - the SAX event handler
skipBinary - flag for ignoring binary properties
noRecurse - flag for not exporting an entire content subtree
Method Detail

visit

public final void visit(Property property)
Ignored. Properties are included as attributes of node elements.

Specified by:
visit in interface ItemVisitor
Parameters:
property - ignored property
See Also:
ItemVisitor.visit(Property)

visit

public final void visit(Node node)
                 throws RepositoryException
Exports the visited node using the document view serialization format. This method is the main entry point to the serialization mechanism. It manages the opening and closing of the SAX event stream and the registration of the namespace mappings. The process of actually generating the document view SAX events is spread into various private methods, and can be controlled by overriding the protected includeProperty() and includeNode() methods.

Specified by:
visit in interface ItemVisitor
Parameters:
node - the node to visit
Throws:
RepositoryException - on repository errors
See Also:
ItemVisitor.visit(Node), includeProperty(Property), includeNode(Node)

includeProperty

protected boolean includeProperty(Property property)
                           throws RepositoryException
Checks whether the given property should be included in the XML serialization. This method returns true by default, but subclasses can override this method to implement more selective XML serialization.

Parameters:
property - the property to check
Returns:
true if the property should be included, false otherwise
Throws:
RepositoryException - on repository errors

includeNode

protected boolean includeNode(Node node)
                       throws RepositoryException
Checks whether the given node should be included in the XML serialization. This method returns true by default, but subclasses override this method to implement selective XML serialization.

Note that this method is only called for the descendants of the root node of the serialized tree. Also, this method is never called if the noRecurse flag is set because no descendant nodes will be serialized anyway.

Parameters:
node - the node to check
Returns:
true if the node should be included, false otherwise
Throws:
RepositoryException - on repository errors


Copyright © 2004-2005 . All Rights Reserved.