Package org.opencms.file
Class CmsLinkRewriter
java.lang.Object
org.opencms.file.CmsLinkRewriter
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
Modifier and TypeFieldDescriptionprotected Map<CmsUUID,
CmsResource> A map from source folder structure ids to corresponding target folder resources.protected Map<String,
CmsResource> A map from source folder root paths to the corresponding target folder resources. -
Constructor Summary
ConstructorDescriptionCmsLinkRewriter
(CmsObject cms, String source, String target) Creates a link rewriter for use after a single copy operation.Creates a new link rewriter for a list of sources and corresponding targets.CmsLinkRewriter
(CmsObject cms, List<String> sources, String target) Creates a link rewriter for use after a multi-copy operation. -
Method Summary
Modifier and TypeMethodDescriptionprotected 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 void
Separate method for copying locale relations..protected String
Decodes a byte array into a string with a given encoding, or the default encoding if that fails.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>
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>
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
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.
-
Field Details
-
m_translationsById
A map from source folder structure ids to corresponding target folder resources. -
m_translationsByPath
A map from source folder root paths to the corresponding target folder resources.
-
-
Constructor Details
-
CmsLinkRewriter
Creates a link rewriter for use after a multi-copy operation.- Parameters:
cms
- the current CMS contextsources
- the list of source root pathstarget
- 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 contexttargetPath
- the target root pathsourceTargetPairs
- the list of source-target pairs
-
CmsLinkRewriter
Creates a link rewriter for use after a single copy operation.- Parameters:
cms
- the current CMS contextsource
- the source folder root pathtarget
- the target folder root path
-
-
Method Details
-
checkIsFolder
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
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
Starts the link rewriting process.- Throws:
CmsException
- if something goes wrong
-
setRewriteAllContents
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
Checks that the target path is not a subfolder of the source path.- Parameters:
source
- the source pathtarget
- the target path
-
copyLocaleRelations
Separate method for copying locale relations..This is necessary because the default copy mechanism does not copy locale relations.
- Throws:
CmsException
- if something goes wrong
-
decode
Decodes a byte array into a string with a given encoding, or the default encoding if that fails.- Parameters:
bytes
- the byte arrayencoding
- the encoding to use- Returns:
- the decoded string
-
decode
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
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
Gets the encoding which is configured at the location of a given resource.- Parameters:
cms
- the current CMS contextresource
- 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 roottarget
- the target root- Returns:
- the list of matching resources
- Throws:
CmsException
- if something goes wrong
-
getRelativePath
Computes the relative path given an ancestor folder path.- Parameters:
ancestor
- the ancestor folderrootPath
- the path for which the relative path should be computed- Returns:
- the relative path
-
getResource
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 mapbasePath
- the path relative to which the keys of the resulting map should be computed- Returns:
- a map from relative paths to resources
-
init
Reads the data needed for rewriting the relations from the VFS.- Throws:
CmsException
- if something goes wrong
-
isInSources
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
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
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
Rewrites the links included in the content itself.- Parameters:
file
- the file for which the links should be replacedrelations
- the original relations- Throws:
CmsException
- if something goes wrong
-
rewriteContentString
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 rewrittenrelations
- 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 relationsrelations
- the original relations- Throws:
CmsException
- if something goes wrong
-