org.apache.jackrabbit.xml
Class SystemViewExportVisitor

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

public class SystemViewExportVisitor
extends Object
implements ItemVisitor

Generic system view exporter for JCR content repositories. This class can be used to implement the XML system view export operations using nothing but the standard JCR interfaces. The export operation is implemented as an ItemVisitor that generates the system 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 SystemViewExportVisitor(handler, true, false) {

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

             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.exportSysView(String, ContentHandler, boolean, boolean) method implemented in terms of this exporter class:

     public void exportSysView(String absPath, ContentHandler handler,
             boolean skipBinary, boolean noRecurse) throws
             InvalidSerializedDataException, 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.exportSysView(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 exportSysView(String absPath, OutputStream output,
             boolean skipBinary, boolean noRecurse) throws
             InvalidSerializedDataException, PathNotFoundException,
             IOException, RepositoryException {
         try {
             XMLSerializer serializer =
                 new XMLSerializer(output, new OutputFormat());
             exportSysView(absPath, serializer.asContentHandler(),
                     binaryAsLink, noRecurse);
         } catch (SAXException ex) {
             throw new IOException(ex.getMessage());
         }
     }
 

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

Field Summary
protected  Node root
          The root node of the serialization tree.
 
Constructor Summary
SystemViewExportVisitor(ContentHandler handler, boolean skipBinary, boolean noRecurse)
          Creates an visitor for exporting content using the system 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 system view serialization format.
 void visit(Property property)
          Exports the visited property using the system view serialization format.
 
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

SystemViewExportVisitor

public SystemViewExportVisitor(ContentHandler handler,
                               boolean skipBinary,
                               boolean noRecurse)
Creates an visitor for exporting content using the system 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 void visit(Property property)
           throws RepositoryException
Exports the visited property using the system view serialization format. This method generates an sv:property element with appropriate sv:name and sv:type attributes. The value or values of the node are included as sv:value sub-elements.

Specified by:
visit in interface ItemVisitor
Parameters:
property - the visited property
Throws:
RepositoryException - on repository errors

visit

public void visit(Node node)
           throws RepositoryException
Exports the visited node using the system 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

includeProperty

protected boolean includeProperty(Property property)
                           throws RepositoryException
Checks whether the given property should be included in the XML serialization. By default this method returns false for the special jcr:primaryType, jcr:mixinTypes, and jcr:uuid properties that are required by the system view format. This method also returns false for all binary properties if the skipBinary flag is set. Subclasses can extend this default behaviour to implement more selective XML serialization.

To avoid losing the default behaviour described above, subclasses should always call super.includeProperty(property) instead of simply returning true for a property.

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 can extend this behaviour 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.