org.opencms.file
Class CmsLinkRewriter

java.lang.Object
  extended by org.opencms.file.CmsLinkRewriter

public class CmsLinkRewriter
extends Object

A class used to rewrite links and relations in one subtree such that relations from that subtree to another given subtree replaced with relations to the first subtree.


Field Summary
protected  Map<CmsUUID,CmsResource> m_translationsById
          A map from source folder structure ids to corresponding target folder resources.
protected  Map<String,CmsResource> m_translationsByPath
          A map from source folder root paths to the corresponding target folder resources.
 
Constructor Summary
CmsLinkRewriter(CmsObject cms, List<String> sources, String target)
          Creates a link rewriter for use after a multi-copy operation.
CmsLinkRewriter(CmsObject cms, String targetPath, List<CmsPair<String,String>> sourceTargetPairs)
          Creates a new link rewriter for a list of sources and corresponding targets.
CmsLinkRewriter(CmsObject cms, String source, String target)
          Creates a link rewriter for use after a single copy operation.
 
Method Summary
protected static void checkIsFolder(CmsResource resource)
          Checks whether a given resource is a folder and throws an exception otherwise.
protected  void checkNotSubPath(String source, String target)
          Checks that the target path is not a subfolder of the source path.
protected  String decode(byte[] bytes, String encoding)
          Decodes a byte array into a string with a given encoding, or the default encoding if that fails.
protected  CmsPair<String,String> decode(CmsFile file)
          Decodes a file's contents and return the content string and the encoding to use for writing the file back to the VFS.
protected  List<CmsRelation> findRelationsFromTargetToSource()
          Finds relations from the target root folder or its children to the source root folder or its children.
protected  String getConfiguredEncoding(CmsObject cms, CmsResource resource)
          Gets the encoding which is configured at the location of a given resource.
protected  List<CmsPair<CmsResource,CmsResource>> getMatchingResources(String source, String target)
          Gets a list of resource pairs whose paths relative to the source/target roots passed match.
protected  String getRelativePath(String ancestor, String rootPath)
          Computes the relative path given an ancestor folder path.
protected  CmsResource getResource(CmsUUID structureId)
          Accesses a resource by structure id.
protected  Map<String,CmsResource> getResourcesByRelativePath(List<CmsResource> resources, String basePath)
          Collects a list of resources in a map where the key for each resource is the path relative to a given folder.
protected  void init()
          Reads the data needed for rewriting the relations from the VFS.
protected static boolean isFolder(CmsResource resource)
          Helper method to check whether a given resource is a folder.
protected  boolean isInSources(String path)
          Checks if a path belongs to one of the sources.
protected  boolean isInTargets(String path)
          Checks if a path belongs to one of the targets.
protected  List<CmsResource> readTree(String rootPath)
          Reads the resources in a subtree.
protected  void rewriteContent(CmsFile file, Collection<CmsRelation> relations)
          Rewrites the links included in the content itself.
protected  String rewriteContentString(String originalContent)
          Replaces structure ids of resources in the source subtree with the structure ids of the corresponding resources in the target subtree inside a content string.
 void rewriteLinks()
          Starts the link rewriting process.
protected  void rewriteLinks(CmsResource resource, Collection<CmsRelation> relations)
          Rewrites the links for a single resource.
protected  void rewriteOtherRelations(CmsResource res, Collection<CmsRelation> relations)
          Rewrites relations which are not derived from links in the content itself.
 void setRewriteAllContents(boolean rewriteAllContents)
          Sets the 'rewriteAllContents' flag, which controls whether all XML contents will be rewritten or just those whose links need to be corrected.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_translationsById

protected Map<CmsUUID,CmsResource> m_translationsById
A map from source folder structure ids to corresponding target folder resources.


m_translationsByPath

protected Map<String,CmsResource> m_translationsByPath
A map from source folder root paths to the corresponding target folder resources.

Constructor Detail

CmsLinkRewriter

public CmsLinkRewriter(CmsObject cms,
                       List<String> sources,
                       String target)
Creates a link rewriter for use after a multi-copy operation.

Parameters:
cms - the current CMS context
sources - the list of source root paths
target - the target parent folder root path

CmsLinkRewriter

public CmsLinkRewriter(CmsObject cms,
                       String targetPath,
                       List<CmsPair<String,String>> sourceTargetPairs)
Creates a new link rewriter for a list of sources and corresponding targets.

Parameters:
cms - the current CMS context
targetPath - the target root path
sourceTargetPairs - the list of source-target pairs

CmsLinkRewriter

public CmsLinkRewriter(CmsObject cms,
                       String source,
                       String target)
Creates a link rewriter for use after a single copy operation.

Parameters:
cms - the current CMS context
source - the source folder root path
target - the target folder root path
Method Detail

checkIsFolder

protected static void checkIsFolder(CmsResource resource)
                             throws CmsException
Checks whether a given resource is a folder and throws an exception otherwise.

Parameters:
resource - the resource to check
Throws:
CmsException - if something goes wrong

isFolder

protected static boolean isFolder(CmsResource resource)
                           throws CmsLoaderException
Helper method to check whether a given resource is a folder.

Parameters:
resource - the resouce to check
Returns:
true if the resource is a folder
Throws:
CmsLoaderException - if the resource type couldn't be found

rewriteLinks

public void rewriteLinks()
                  throws CmsException
Starts the link rewriting process.

Throws:
CmsException - if something goes wrong

setRewriteAllContents

public void setRewriteAllContents(boolean rewriteAllContents)
Sets the 'rewriteAllContents' flag, which controls whether all XML contents will be rewritten or just those whose links need to be corrected.

Parameters:
rewriteAllContents - if true, all contents will be rewritten

checkNotSubPath

protected void checkNotSubPath(String source,
                               String target)
Checks that the target path is not a subfolder of the source path.

Parameters:
source - the source path
target - the target path

decode

protected String decode(byte[] bytes,
                        String encoding)
Decodes a byte array into a string with a given encoding, or the default encoding if that fails.

Parameters:
bytes - the byte array
encoding - the encoding to use
Returns:
the decoded string

decode

protected CmsPair<String,String> decode(CmsFile file)
                                 throws CmsException
Decodes a file's contents and return the content string and the encoding to use for writing the file back to the VFS.

Parameters:
file - the file to decode
Returns:
a pair (content, encoding)
Throws:
CmsException - if something goes wrong

findRelationsFromTargetToSource

protected List<CmsRelation> findRelationsFromTargetToSource()
                                                     throws CmsException
Finds relations from the target root folder or its children to the source root folder or its children.

Returns:
the list of relations from the target to the source
Throws:
CmsException - if something goes wrong

getConfiguredEncoding

protected String getConfiguredEncoding(CmsObject cms,
                                       CmsResource resource)
                                throws CmsException
Gets the encoding which is configured at the location of a given resource.

Parameters:
cms - the current CMS context
resource - the resource for which the configured encoding should be retrieved
Returns:
the configured encoding for the resource
Throws:
CmsException - if something goes wrong

getMatchingResources

protected List<CmsPair<CmsResource,CmsResource>> getMatchingResources(String source,
                                                                      String target)
                                                               throws CmsException
Gets a list of resource pairs whose paths relative to the source/target roots passed match.

Parameters:
source - the source root
target - the target root
Returns:
the list of matching resources
Throws:
CmsException - if something goes wrong

getRelativePath

protected String getRelativePath(String ancestor,
                                 String rootPath)
Computes the relative path given an ancestor folder path.

Parameters:
ancestor - the ancestor folder
rootPath - the path for which the relative path should be computed
Returns:
the relative path

getResource

protected CmsResource getResource(CmsUUID structureId)
                           throws CmsException
Accesses a resource by structure id.

Parameters:
structureId - the structure id of the resource
Returns:
the resource with the given structure id
Throws:
CmsException - if the resource couldn't be read

getResourcesByRelativePath

protected Map<String,CmsResource> getResourcesByRelativePath(List<CmsResource> resources,
                                                             String basePath)
Collects a list of resources in a map where the key for each resource is the path relative to a given folder.

Parameters:
resources - the resources to put in the map
basePath - the path relative to which the keys of the resulting map should be computed
Returns:
a map from relative paths to resources

init

protected void init()
             throws CmsException
Reads the data needed for rewriting the relations from the VFS.

Throws:
CmsException - if something goes wrong

isInSources

protected boolean isInSources(String path)
Checks if a path belongs to one of the sources.

Parameters:
path - a root path
Returns:
true if the path belongs to the sources

isInTargets

protected boolean isInTargets(String path)
Checks if a path belongs to one of the targets.

Parameters:
path - a root path
Returns:
true if the path belongs to the targets

readTree

protected List<CmsResource> readTree(String rootPath)
                              throws CmsException
Reads the resources in a subtree.

Parameters:
rootPath - the root of the subtree
Returns:
the list of resources from the subtree
Throws:
CmsException - if something goes wrong

rewriteContent

protected void rewriteContent(CmsFile file,
                              Collection<CmsRelation> relations)
                       throws CmsException
Rewrites the links included in the content itself.

Parameters:
file - the file for which the links should be replaced
relations - the original relations
Throws:
CmsException - if something goes wrong

rewriteContentString

protected String rewriteContentString(String originalContent)
Replaces structure ids of resources in the source subtree with the structure ids of the corresponding resources in the target subtree inside a content string.

Parameters:
originalContent - the original content
Returns:
the content with the new structure ids

rewriteLinks

protected void rewriteLinks(CmsResource resource,
                            Collection<CmsRelation> relations)
                     throws CmsException
Rewrites the links for a single resource.

Parameters:
resource - the resource for which the links should be rewritten
relations - the relations to the source folder which have this resource as its source
Throws:
CmsException - if something goes wrong

rewriteOtherRelations

protected void rewriteOtherRelations(CmsResource res,
                                     Collection<CmsRelation> relations)
                              throws CmsException
Rewrites relations which are not derived from links in the content itself.

Parameters:
res - the resource for which to rewrite the relations
relations - the original relations
Throws:
CmsException - if something goes wrong