org.modeshape.jcr
Class JcrTools

java.lang.Object
  extended by org.modeshape.jcr.JcrTools

public class JcrTools
extends Object

Utility methods for working with JCR nodes.


Constructor Summary
JcrTools()
           
 
Method Summary
 String detectMimeType(Session session, File file, boolean useContent)
          Detect the MIME type for the named filename.
 String detectMimeType(Session session, String fileName)
          Detect the MIME type for the named filename.
 String detectMimeType(Session session, URL url, boolean useContent)
          Detect the MIME type for the named filename.
 Node findOrCreateChild(Node parent, String name)
          Get or create a node with the specified node under the specified parent node.
 Node findOrCreateChild(Node parent, String name, String nodeType)
          Get or create a node with the specified node and node type under the specified parent node.
 Node findOrCreateNode(Node parentNode, String path, String defaultNodeType, String finalNodeType)
          Get or create a node at the specified path.
 Node findOrCreateNode(Session session, String path)
          Get or create a node at the specified path.
 Node findOrCreateNode(Session session, String path, String nodeType)
          Get or create a node at the specified path and node type.
 Node findOrCreateNode(Session session, String path, String defaultNodeType, String finalNodeType)
          Get or create a node at the specified path.
 Node getNode(Node node, String relativePath, boolean required)
          Get the node under a specified node at a location defined by the specified relative path.
 String getReadable(Node node)
          Get the readable string form for a specified node.
 MimeTypeDetector mimeTypeDetector(Session session)
           
 int removeAllChildren(Node node)
          Remove all children from the specified node
 Node uploadFile(Session session, String path, File file)
          Upload the content in the supplied file into the repository at the defined path, using the given session.
 Node uploadFile(Session session, String path, InputStream stream)
          Upload the content in the supplied stream into the repository at the defined path, using the given session.
 Node uploadFile(Session session, String path, URL contentUrl)
          Upload the content at the supplied URL into the repository at the defined path, using the given session.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

JcrTools

public JcrTools()
Method Detail

removeAllChildren

public int removeAllChildren(Node node)
                      throws RepositoryException
Remove all children from the specified node

Parameters:
node -
Returns:
the number of children removed.
Throws:
RepositoryException
IllegalArgumentException - if the node argument is null

getNode

public Node getNode(Node node,
                    String relativePath,
                    boolean required)
             throws RepositoryException
Get the node under a specified node at a location defined by the specified relative path. If node is required, then a problem is created and added to the Problems list.

Parameters:
node - a parent node from which to obtain a node relative to. may not be null
relativePath - the path of the desired node. may not be null
required - true if node is required to exist under the given node.
Returns:
the node located relative the the input node
Throws:
RepositoryException
IllegalArgumentException - if the node, relativePath or problems argument is null

getReadable

public String getReadable(Node node)
Get the readable string form for a specified node.

Parameters:
node - the node to obtain the readable string form. may be null
Returns:
the readable string form for a specified node.

mimeTypeDetector

public MimeTypeDetector mimeTypeDetector(Session session)

detectMimeType

public String detectMimeType(Session session,
                             String fileName)
Detect the MIME type for the named filename.

Parameters:
session - the JCR session
fileName - the file name
Returns:
the MIME type

detectMimeType

public String detectMimeType(Session session,
                             File file,
                             boolean useContent)
                      throws IOException
Detect the MIME type for the named filename.

Parameters:
session - the JCR session
file - the file
useContent - true if the content of the file at the URL should also be used to determine the MIME type, or false if only the URL itself should be used
Returns:
the MIME type
Throws:
IOException - if there is an error reading the file

detectMimeType

public String detectMimeType(Session session,
                             URL url,
                             boolean useContent)
                      throws IOException
Detect the MIME type for the named filename.

Parameters:
session - the JCR session
url - the URL
useContent - true if the content of the file at the URL should also be used to determine the MIME type, or false if only the URL itself should be used
Returns:
the MIME type
Throws:
IOException - if there is an error reading the file

uploadFile

public Node uploadFile(Session session,
                       String path,
                       InputStream stream)
                throws RepositoryException,
                       IOException
Upload the content in the supplied stream into the repository at the defined path, using the given session. This method will create a 'nt:file' node at the supplied path, and any non-existant ancestors with nodes of type 'nt:folder'. As defined by the JCR specification, the binary content (and other properties) will be placed on a child of the 'nt:file' node named 'jcr:content' with a node type of 'nt:resource'.

This method always closes the supplied stream.

Parameters:
session - the JCR session
path - the path to the file
stream - the stream containing the content to be uploaded
Returns:
the newly created 'nt:file' node
Throws:
RepositoryException - if there is a problem uploading the file
IOException - if there is a problem using the stream

uploadFile

public Node uploadFile(Session session,
                       String path,
                       URL contentUrl)
                throws RepositoryException,
                       IOException
Upload the content at the supplied URL into the repository at the defined path, using the given session. This method will create a 'nt:file' node at the supplied path, and any non-existant ancestors with nodes of type 'nt:folder'. As defined by the JCR specification, the binary content (and other properties) will be placed on a child of the 'nt:file' node named 'jcr:content' with a node type of 'nt:resource'.

Parameters:
session - the JCR session
path - the path to the file
contentUrl - the URL where the content can be found
Returns:
the newly created 'nt:file' node
Throws:
RepositoryException - if there is a problem uploading the file
IOException - if there is a problem using the stream

uploadFile

public Node uploadFile(Session session,
                       String path,
                       File file)
                throws RepositoryException,
                       IOException
Upload the content in the supplied file into the repository at the defined path, using the given session. This method will create a 'nt:file' node at the supplied path, and any non-existant ancestors with nodes of type 'nt:folder'. As defined by the JCR specification, the binary content (and other properties) will be placed on a child of the 'nt:file' node named 'jcr:content' with a node type of 'nt:resource'.

Parameters:
session - the JCR session
path - the path to the file
file - the existing and readable file to be uploaded
Returns:
the newly created 'nt:file' node
Throws:
RepositoryException - if there is a problem uploading the file
IOException - if there is a problem using the stream
IllegalArgumentException - if the file does not exist or is not readable

findOrCreateNode

public Node findOrCreateNode(Session session,
                             String path)
                      throws RepositoryException
Get or create a node at the specified path.

Parameters:
session - the JCR session. may not be null
path - the path of the desired node to be found or created. may not be null
Returns:
the existing or newly created node
Throws:
RepositoryException
IllegalArgumentException - if either the session or path argument is null

findOrCreateNode

public Node findOrCreateNode(Session session,
                             String path,
                             String nodeType)
                      throws RepositoryException
Get or create a node at the specified path and node type.

Parameters:
session - the JCR session. may not be null
path - the path of the desired node to be found or created. may not be null
nodeType - the node type. may be null
Returns:
the existing or newly created node
Throws:
RepositoryException
IllegalArgumentException - if either the session or path argument is null

findOrCreateNode

public Node findOrCreateNode(Session session,
                             String path,
                             String defaultNodeType,
                             String finalNodeType)
                      throws RepositoryException
Get or create a node at the specified path.

Parameters:
session - the JCR session. may not be null
path - the path of the desired node to be found or created. may not be null
defaultNodeType - the default node type. may be null
finalNodeType - the optional final node type. may be null
Returns:
the existing or newly created node
Throws:
RepositoryException
IllegalArgumentException - if either the session or path argument is null

findOrCreateNode

public Node findOrCreateNode(Node parentNode,
                             String path,
                             String defaultNodeType,
                             String finalNodeType)
                      throws RepositoryException
Get or create a node at the specified path.

Parameters:
parentNode - the parent node. may not be null
path - the path of the desired child node. may not be null
defaultNodeType - the default node type. may be null
finalNodeType - the optional final node type. may be null
Returns:
the existing or newly created node
Throws:
RepositoryException
IllegalArgumentException - if either the parentNode or path argument is null

findOrCreateChild

public Node findOrCreateChild(Node parent,
                              String name)
                       throws RepositoryException
Get or create a node with the specified node under the specified parent node.

Parameters:
parent - the parent node. may not be null
name - the name of the child node. may not be null
Returns:
the existing or newly created child node
Throws:
RepositoryException
IllegalArgumentException - if either the parent or name argument is null

findOrCreateChild

public Node findOrCreateChild(Node parent,
                              String name,
                              String nodeType)
                       throws RepositoryException
Get or create a node with the specified node and node type under the specified parent node.

Parameters:
parent - the parent node. may not be null
name - the name of the child node. may not be null
nodeType - the node type. may be null
Returns:
the existing or newly created child node
Throws:
RepositoryException


Copyright © 2008-2010 JBoss, a division of Red Hat. All Rights Reserved.