com.xml_project.morganaxproc.filesystem
Class AlternateFilesystem

java.lang.Object
  extended by com.xml_project.morganaxproc.filesystem.XProcFilesystem
      extended by com.xml_project.morganaxproc.filesystem.AlternateFilesystem
All Implemented Interfaces:
CollectionURIResolver, ModuleURIResolver, org.xml.sax.EntityResolver
Direct Known Subclasses:
DefaultFilesystem

public class AlternateFilesystem
extends XProcFilesystem

This class provides the basic stream-/file access functionality needed in MorganaXProc by implementing the abstract classes of XProcFilesystem with the notable exception of httpRequest which is just implemented by throwing an UnknownSchemeException. This leads to an activation of the "switch back"-feature of doHttpRequest(XProcHttpRequest) in XProcFilesystem (see there).

AlternateFilesystem gets it's name from the standard mechanism for choosing an implementation for XProcFilesystem used by MorganaXProc. It first tries first to access DefaultFilesystem (which provides also a working implementation for p:http-request). If this fails due to missing external java libs, MorganaXProc switches back to AlternateFilesystem. So while this class in the first in order of extending/implementing XProcFilesystem it is only the second best choice when running MorganaXProc.

This class supports stream access for the following schemes: 'file:', 'http:' and 'https:' realized via URLConnection or the standard java.io-package (for 'file:'). Credentials must be set by using java.net.Authenticator.setDefault. getDirectoryList(URI) is only implemented for "file:"-scheme.

To add support for additional uri schemes just extend this class, test whether the uri has one of the schemes your implementation supports or fall back to this implementation for the standard schemes. Of course you can also implement your own extension of XProcFilesystem without any reference to this class or just use the implementation in StandardFilesystem as your base.


Nested Class Summary
 
Nested classes/interfaces inherited from class com.xml_project.morganaxproc.filesystem.XProcFilesystem
XProcFilesystem.DefectiveURIException, XProcFilesystem.LoadResult, XProcFilesystem.ParseMode, XProcFilesystem.ProtectedResourceException, XProcFilesystem.StreamResult, XProcFilesystem.UnknownSchemeException, XProcFilesystem.UnsupportedMethodException, XProcFilesystem.UnsupportedXMLVersionException
 
Field Summary
 
Fields inherited from class com.xml_project.morganaxproc.filesystem.XProcFilesystem
DIRECTORY_MARKER, theConfig
 
Constructor Summary
AlternateFilesystem(XProcConfiguration config)
          Creates a new instance of AlternateFilesystem.
 
Method Summary
protected  void clearCookieStore()
          Just an empty method, for there is no support for p:http-request in this class.
protected  java.net.URI copyResource(java.net.URI source, java.net.URI target)
          Copies resource source to target.
protected  java.net.URI createDirectory(java.net.URI uri)
          Create the directory names by the uri.
protected  java.net.URI createTemp(java.net.URI folderURI, boolean createFolder, java.lang.String prefix, java.lang.String suffix, boolean deleteOnExit)
          Create Create a temp file in the directory named by uri.
protected  void deleteResource(java.net.URI uri, boolean recursive)
          Implementation for file:
protected  java.lang.String getContentType(java.net.URI uri)
          Returns the content type for the resource with the given uri.
protected  java.lang.String[] getDirectoryList(java.net.URI uri)
          Returns a list of the resource names found in the directory found on the given uri only if it has a 'file:' scheme.
protected  java.lang.String getEncoding(java.net.URI uri)
          Returns the encoding for uri schemes 'file:/', 'http:' and 'https:' throws UnknownSchemeException for other schemes.
protected  XProcFilesystem.StreamResult getInputStream(java.net.URI uri)
          Return a StreamResult for the given uri if it has one of supported schemes 'file:', 'http:' or 'https:'.
protected  java.io.OutputStream getOutputStream(java.net.URI uri)
          Return an OutputStream for the given uri, but with different behavior according to the scheme: For 'file:' a regular FileOutputStream is provided, that can be used for writing data to a medium.
protected  nu.xom.Document getResourceInfo(java.net.URI uri, boolean writeAllowed)
          Implementation for file and http(s): Supported standard attributes for "file" are Readable, Writable, Hidden, LastModified and Size.
protected  XProcHttpResponse httpRequest(XProcHttpRequest request)
          Always throws an UnknownSchemeException since no support for p:http-request is provided by this class.
protected  java.net.URI moveResource(java.net.URI source, java.net.URI target)
          Moves file source to target.
protected  java.net.URI touchResource(java.net.URI uri, long timeStamp)
           
 
Methods inherited from class com.xml_project.morganaxproc.filesystem.XProcFilesystem
build, copy, copyResourceChecked, createDirectoryChecked, createTempChecked, deleteResourceChecked, doHttpRequest, getAsStreamSource, getDirectoryList, getDocument, getLoadResult, getResourceInfoChecked, loadDocument, loadDocument, loadDocumentFromPackage, moveResourceChecked, resolveAsStreamSource, resolveCollection, resolveEntity, resolveModule, resolveText, resolveURI, touchResourceChecked, writeData, writeData
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AlternateFilesystem

public AlternateFilesystem(XProcConfiguration config)
Creates a new instance of AlternateFilesystem.

Parameters:
config -
Method Detail

getInputStream

protected XProcFilesystem.StreamResult getInputStream(java.net.URI uri)
                                               throws java.io.IOException,
                                                      XProcFilesystem.UnknownSchemeException,
                                                      XProcFilesystem.ProtectedResourceException
Return a StreamResult for the given uri if it has one of supported schemes 'file:', 'http:' or 'https:'. For any other scheme, an UnknownSchemeException is thrown. For 'http:' and 'https:' a ProtectedResourceException is thrown if the resource can only be accessed by providing credentials (HTTP response code 401).

Specified by:
getInputStream in class XProcFilesystem
Parameters:
uri - the uri from which the StreamResult should be taken.
Returns:
the StreamResult an the given uri.
Throws:
java.io.IOException - if an error occurred reading the uri.
XProcFilesystem.UnknownSchemeException - if the uri's scheme is unknown or not supported.
XProcFilesystem.ProtectedResourceException - if the resource with the given uri cannot be obtained without authentication (HTPP status code 401).

getOutputStream

protected java.io.OutputStream getOutputStream(java.net.URI uri)
                                        throws java.io.IOException,
                                               XProcFilesystem.UnknownSchemeException,
                                               XProcFilesystem.ProtectedResourceException
Return an OutputStream for the given uri, but with different behavior according to the scheme: For 'file:' a regular FileOutputStream is provided, that can be used for writing data to a medium. If the scheme is 'http:' or 'https:' a request with http method 'post' is issued. Again: a ProtectedResourceException is thrown if the resource can only be accessed by providing credentials (HTTP response code 401).

Specified by:
getOutputStream in class XProcFilesystem
Parameters:
uri - the uri for which the OutputStream is requested.
Returns:
the requested output stream.
Throws:
java.io.IOException - if an error occurred making the OutputStream for the uri.
XProcFilesystem.UnknownSchemeException - if the uri's scheme is unknown or not supported.
XProcFilesystem.ProtectedResourceException - if the resource with the given uri cannot be obtained without authentication (HTPP status code 401).

getContentType

protected java.lang.String getContentType(java.net.URI uri)
                                   throws java.io.IOException,
                                          XProcFilesystem.UnknownSchemeException,
                                          XProcFilesystem.ProtectedResourceException
Returns the content type for the resource with the given uri. For scheme 'file:' the content type is guessed by inspecting the name of file (especially the extension). If the scheme is not implemented, an UnknownSchemeException is thrown. A ProtectedResourceException is thrown if the resource can only be accessed by providing credentials (HTTP response code 401).

Specified by:
getContentType in class XProcFilesystem
Parameters:
uri - the uri for which the content type is requested.
Returns:
the requested content type.
Throws:
java.io.IOException - if any error occurred while requesting the content type.
XProcFilesystem.UnknownSchemeException - if the uri's scheme is unknown or not supported.
XProcFilesystem.ProtectedResourceException - if the resource with the given uri cannot be obtained without authentication (HTPP status code 401).

getEncoding

protected java.lang.String getEncoding(java.net.URI uri)
                                throws java.io.IOException,
                                       XProcFilesystem.UnknownSchemeException,
                                       XProcFilesystem.ProtectedResourceException
Returns the encoding for uri schemes 'file:/', 'http:' and 'https:' throws UnknownSchemeException for other schemes. A ProtectedResourceException is thrown if the resource can only be accessed by providing credentials (HTTP response code 401).

Specified by:
getEncoding in class XProcFilesystem
Parameters:
uri - the uri for which the encoding is requested.
Returns:
the requested encoding.
Throws:
java.io.IOException - if any error occurred while requesting the encoding.
XProcFilesystem.UnknownSchemeException - if the uri's scheme is unknown or not supported.
XProcFilesystem.ProtectedResourceException - if the resource with the given uri cannot be obtained without authentication (HTPP status code 401).

httpRequest

protected XProcHttpResponse httpRequest(XProcHttpRequest request)
                                 throws java.io.IOException,
                                        XProcFilesystem.UnknownSchemeException
Always throws an UnknownSchemeException since no support for p:http-request is provided by this class.

Specified by:
httpRequest in class XProcFilesystem
Parameters:
request - the request to answer.
Returns:
the XProcHttpResponse for this request.
Throws:
java.io.IOException - if an IOException occurred.
XProcFilesystem.UnknownSchemeException - if the uri's scheme is unknown or not supported.

clearCookieStore

protected void clearCookieStore()
Just an empty method, for there is no support for p:http-request in this class.

Specified by:
clearCookieStore in class XProcFilesystem

createTemp

protected java.net.URI createTemp(java.net.URI folderURI,
                                  boolean createFolder,
                                  java.lang.String prefix,
                                  java.lang.String suffix,
                                  boolean deleteOnExit)
                           throws XProcFilesystem.UnknownSchemeException,
                                  java.io.FileNotFoundException,
                                  java.io.IOException
Description copied from class: XProcFilesystem
Create Create a temp file in the directory named by uri.

Specified by:
createTemp in class XProcFilesystem
Parameters:
folderURI - the directory in which the temp file should be created.
prefix - the file names prefix or null.
suffix - the file names suffix or null.
deleteOnExit - true, if the created file should be deleted when the processor terminates.
Returns:
the absolute uri of the created temp file
Throws:
XProcFilesystem.UnknownSchemeException
java.io.FileNotFoundException
java.io.IOException

createDirectory

protected java.net.URI createDirectory(java.net.URI uri)
                                throws XProcFilesystem.UnknownSchemeException,
                                       java.io.IOException
Description copied from class: XProcFilesystem
Create the directory names by the uri.

Specified by:
createDirectory in class XProcFilesystem
Returns:
the absolute uri of the created directory.
Throws:
XProcFilesystem.UnknownSchemeException
java.io.IOException

touchResource

protected java.net.URI touchResource(java.net.URI uri,
                                     long timeStamp)
                              throws XProcFilesystem.UnknownSchemeException,
                                     java.io.FileNotFoundException,
                                     java.io.IOException
Specified by:
touchResource in class XProcFilesystem
timeStamp - value.
Returns:
the absolute uri of the touched resource.
Throws:
XProcFilesystem.UnknownSchemeException
java.io.FileNotFoundException
java.io.IOException

moveResource

protected java.net.URI moveResource(java.net.URI source,
                                    java.net.URI target)
                             throws XProcFilesystem.UnknownSchemeException,
                                    java.io.FileNotFoundException,
                                    java.io.IOException
Description copied from class: XProcFilesystem
Moves file source to target. If target names a folder, the copy is put into this folder. If a file with target already exists, it is overwritten.

Specified by:
moveResource in class XProcFilesystem
Returns:
the absolute uri of the target.
Throws:
XProcFilesystem.UnknownSchemeException
java.io.FileNotFoundException
java.io.IOException

copyResource

protected java.net.URI copyResource(java.net.URI source,
                                    java.net.URI target)
                             throws XProcFilesystem.UnknownSchemeException,
                                    java.io.FileNotFoundException,
                                    java.io.IOException
Description copied from class: XProcFilesystem
Copies resource source to target. If target names a folder, the copy is put into this folder. If a file with target already exists, it is overwritten.

Specified by:
copyResource in class XProcFilesystem
Parameters:
source - a uri designating a file
target - a uri designating a file or a folder
Returns:
the target uri where source was copied to.
Throws:
XProcFilesystem.UnknownSchemeException
java.io.FileNotFoundException
java.io.IOException

deleteResource

protected void deleteResource(java.net.URI uri,
                              boolean recursive)
                       throws XProcFilesystem.UnknownSchemeException,
                              java.io.FileNotFoundException,
                              java.io.IOException
Implementation for file:

Specified by:
deleteResource in class XProcFilesystem
Throws:
XProcFilesystem.UnknownSchemeException
java.io.FileNotFoundException
java.io.IOException

getResourceInfo

protected nu.xom.Document getResourceInfo(java.net.URI uri,
                                          boolean writeAllowed)
                                   throws XProcFilesystem.UnknownSchemeException,
                                          java.io.IOException,
                                          java.io.FileNotFoundException
Implementation for file and http(s): Supported standard attributes for "file" are Readable, Writable, Hidden, LastModified and Size. For additionally mox:content-type is supported, for the attribute "mox:isEmpty" is supported for empty directories. Calling this method with scheme(s) "http(s)" will always return a c:uri element if the host can be found. Attributes are "status" and (if status is "2xx" or "3xx"): Readable, LastModified and Size. Additionally mox:content-type is supported.

Specified by:
getResourceInfo in class XProcFilesystem
Parameters:
uri - the uri to get the information for.
Returns:
the document or null.
Throws:
UnknownSchemeException
java.io.FileNotFoundException
XProcFilesystem.UnknownSchemeException
java.io.IOException

getDirectoryList

protected java.lang.String[] getDirectoryList(java.net.URI uri)
                                       throws java.io.IOException,
                                              XProcFilesystem.UnknownSchemeException,
                                              XProcFilesystem.ProtectedResourceException
Returns a list of the resource names found in the directory found on the given uri only if it has a 'file:' scheme. If the uri does not name a directory, an IOException is thrown. The names appearing in the result are just the file names, i.e. the last segment of the path. If the directory at uri contains directories, the names of this directories must be returned prefix with DIRECTORY_MARKER. For any non 'file:' scheme, an UnknownSchemeException is thrown.

Specified by:
getDirectoryList in class XProcFilesystem
Parameters:
uri - the uri for which the directory listing is requested.
Returns:
a string array which the content of the requested directory.
Throws:
java.io.IOException - if an error occurred reading the resource at uri or if the resource is no directory.
XProcFilesystem.ProtectedResourceException - if the resource with the given uri cannot be obtained without authentication (HTPP status code 401).
XProcFilesystem.UnknownSchemeException - if the uri's scheme is unknown or not supported.