Package org.opencms.file
Class CmsLinkRewriter
- java.lang.Object
-
- org.opencms.file.CmsLinkRewriter
-
public class CmsLinkRewriter extends java.lang.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
Fields Modifier and Type Field Description protected java.util.Map<CmsUUID,CmsResource>
m_translationsById
A map from source folder structure ids to corresponding target folder resources.protected java.util.Map<java.lang.String,CmsResource>
m_translationsByPath
A map from source folder root paths to the corresponding target folder resources.
-
Constructor Summary
Constructors Constructor Description CmsLinkRewriter(CmsObject cms, java.lang.String source, java.lang.String target)
Creates a link rewriter for use after a single copy operation.CmsLinkRewriter(CmsObject cms, java.lang.String targetPath, java.util.List<CmsPair<java.lang.String,java.lang.String>> sourceTargetPairs)
Creates a new link rewriter for a list of sources and corresponding targets.CmsLinkRewriter(CmsObject cms, java.util.List<java.lang.String> sources, java.lang.String target)
Creates a link rewriter for use after a multi-copy operation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static void
checkIsFolder(CmsResource resource)
Checks whether a given resource is a folder and throws an exception otherwise.protected void
checkNotSubPath(java.lang.String source, java.lang.String target)
Checks that the target path is not a subfolder of the source path.protected void
copyLocaleRelations()
Separate method for copying locale relations..protected java.lang.String
decode(byte[] bytes, java.lang.String encoding)
Decodes a byte array into a string with a given encoding, or the default encoding if that fails.protected CmsPair<java.lang.String,java.lang.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 java.util.List<CmsRelation>
findRelationsFromTargetToSource()
Finds relations from the target root folder or its children to the source root folder or its children.protected java.lang.String
getConfiguredEncoding(CmsObject cms, CmsResource resource)
Gets the encoding which is configured at the location of a given resource.protected java.util.List<CmsPair<CmsResource,CmsResource>>
getMatchingResources(java.lang.String source, java.lang.String target)
Gets a list of resource pairs whose paths relative to the source/target roots passed match.protected java.lang.String
getRelativePath(java.lang.String ancestor, java.lang.String rootPath)
Computes the relative path given an ancestor folder path.protected CmsResource
getResource(CmsUUID structureId)
Accesses a resource by structure id.protected java.util.Map<java.lang.String,CmsResource>
getResourcesByRelativePath(java.util.List<CmsResource> resources, java.lang.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(java.lang.String path)
Checks if a path belongs to one of the sources.protected boolean
isInTargets(java.lang.String path)
Checks if a path belongs to one of the targets.protected java.util.List<CmsResource>
readTree(java.lang.String rootPath)
Reads the resources in a subtree.protected void
rewriteContent(CmsFile file, java.util.Collection<CmsRelation> relations)
Rewrites the links included in the content itself.protected java.lang.String
rewriteContentString(java.lang.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, java.util.Collection<CmsRelation> relations)
Rewrites the links for a single resource.protected void
rewriteOtherRelations(CmsResource res, java.util.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 Detail
-
m_translationsById
protected java.util.Map<CmsUUID,CmsResource> m_translationsById
A map from source folder structure ids to corresponding target folder resources.
-
m_translationsByPath
protected java.util.Map<java.lang.String,CmsResource> m_translationsByPath
A map from source folder root paths to the corresponding target folder resources.
-
-
Constructor Detail
-
CmsLinkRewriter
public CmsLinkRewriter(CmsObject cms, java.util.List<java.lang.String> sources, java.lang.String target)
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, java.lang.String targetPath, java.util.List<CmsPair<java.lang.String,java.lang.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
public CmsLinkRewriter(CmsObject cms, java.lang.String source, java.lang.String target)
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 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(java.lang.String source, java.lang.String target)
Checks that the target path is not a subfolder of the source path.- Parameters:
source
- the source pathtarget
- the target path
-
copyLocaleRelations
protected void copyLocaleRelations() throws CmsException
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
protected java.lang.String decode(byte[] bytes, java.lang.String encoding)
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
protected CmsPair<java.lang.String,java.lang.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 java.util.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 java.lang.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 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 java.util.List<CmsPair<CmsResource,CmsResource>> getMatchingResources(java.lang.String source, java.lang.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
protected java.lang.String getRelativePath(java.lang.String ancestor, java.lang.String rootPath)
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
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 java.util.Map<java.lang.String,CmsResource> getResourcesByRelativePath(java.util.List<CmsResource> resources, java.lang.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
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(java.lang.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(java.lang.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 java.util.List<CmsResource> readTree(java.lang.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, java.util.Collection<CmsRelation> relations) throws CmsException
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
protected java.lang.String rewriteContentString(java.lang.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, java.util.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, java.util.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
-
-