Class RepositoryUtilities


  • public class RepositoryUtilities
    extends Object
    A collection of repository related helper methods that make it easier to work with repositories.
    Author:
    Thomas Morgner
    • Method Detail

      • getEntity

        public static ContentEntity getEntity​(Repository repository,
                                              String[] name)
                                       throws ContentIOException
        Returns the content entity for the given path name.
        Parameters:
        repository - the repository from where to retrieve the content entity.
        name - the path name as array of name-segments.
        Returns:
        the entity at the position, never null.
        Throws:
        ContentIOException - if the path did not point to a valid content entity.
        See Also:
        splitPath(String, String)
      • isExistsEntity

        public static boolean isExistsEntity​(Repository repository,
                                             String[] name)
                                      throws ContentIOException
        Checks whether a given pathname points to a valid content entity.
        Parameters:
        repository - the repository from where to retrieve the content entity.
        name - the path name as array of name-segments.
        Returns:
        true, if the entity exists, false otherwise.
        Throws:
        ContentIOException - if an unexpected repository error occured.
        See Also:
        splitPath(String, String)
      • createItem

        public static ContentItem createItem​(Repository repository,
                                             String[] name)
                                      throws ContentIOException
        Tries to create a content item with the given path-name in the repository. This call will succeed if and only if all but the last segment of the name point to Content-Locations and if the content-item does not yet exist.
        Parameters:
        repository - the repository in which a new entity should be created.
        name - the name of the new entity as path name.
        Returns:
        the newly created content-item.
        Throws:
        ContentIOException - if an repository error occured or if the path was not valid.
      • createLocation

        public static ContentLocation createLocation​(Repository repository,
                                                     String[] name)
                                              throws ContentIOException
        Tries to create a content location with the given path-name in the repository. This call will succeed if and only if all but the last segment of the name point to Content-Locations and if the content-entity does not yet exist.
        Parameters:
        repository - the repository in which a new entity should be created.
        name - the name of the new entity as path name.
        Returns:
        the newly created content-location.
        Throws:
        ContentIOException - if an repository error occured or if the path was not valid.
      • splitPath

        public static String[] splitPath​(String name,
                                         String separator)
        Splits a string on the given separator. Multiple occurences of the separator are unified into a single separator.
        Parameters:
        name - the path name.
        separator - the separator on which to split.
        Returns:
        the name as array of atomar path elements.
      • split

        public static String[] split​(String name,
                                     String separator)
        Splits a string on the given separator. Multiple occurences of the separator result in empty strings as path elements in the returned array.
        Parameters:
        name - the path name.
        separator - the separator on which to split.
        Returns:
        the name as array of atomar path elements.
      • buildNameArray

        public static String[] buildNameArray​(ContentEntity entity)
        Builds a absolute pathname for the given entity.
        Parameters:
        entity - the entity for which the pathname should be computed.
        Returns:
        the absolute path.
      • buildName

        public static String buildName​(ContentEntity entity,
                                       String separator)
        Builds a string of an absolute pathname for the given entity and using the given separator to separate filename segments..
        Parameters:
        entity - the entity for which the pathname should be computed.
        separator - the filename separator.
        Returns:
        the absolute path.
      • isInvalidPathName

        public static boolean isInvalidPathName​(String name)
        Checks whether the given entity name is valid for filesystems. This method rejects filenames that either contain a slash ('/') or backslash ('\') which both are commonly used path-separators and it rejects filenames that contain only dots (as the dot names are used as directory traversal names).
        Parameters:
        name - the filename that should be tested. This name must be a single name section, not a full path.
        Returns:
        true, if the pathname is valid, false otherwise.
      • writeAsZip

        public static void writeAsZip​(OutputStream outputStream,
                                      Repository repository)
                               throws IOException,
                                      ContentIOException
        Writes the given repository as ZIP-File into the given output stream.
        Parameters:
        outputStream - the output stream that should receive the zipfile.
        repository - the repository that should be written.
        Throws:
        IOException - if an IO error prevents the writing of the file.
        ContentIOException - if a repository related IO error occurs.
      • writeToZipStream

        public static void writeToZipStream​(ZipOutputStream zipOutputStream,
                                            Repository repository)
                                     throws IOException,
                                            ContentIOException
        Writes the given repository to the given ZIP-output stream.
        Parameters:
        zipOutputStream - the output stream that represents the ZipFile to be generated.
        repository - the repository that should be written.
        Throws:
        IOException - if an IO error prevents the writing of the file.
        ContentIOException - if a repository related IO error occurs.
      • getZipLevel

        public static int getZipLevel​(ContentItem item)
        Computes the declared Zip-Compression level for the given content-item. If the content-items attributes do not contain a definition, the default compression is used instead.
        Parameters:
        item - the content item for which the compression factor should be computed.
        Returns:
        the compression level.
      • getZipMethod

        public static int getZipMethod​(ContentItem item)
        Computes the declared Zip-Compression mode for the given content-item. If the content-items attributes do not contain a valid definition, the default compression is used instead.
        Parameters:
        item - the content item for which the compression mode should be computed.
        Returns:
        the compression mode, either ZipOutputStream.DEFLATED or ZipOutputStream.STORED.