Index: vault-core/src/main/java/org/apache/jackrabbit/vault/fs/api/RegexpPathMapping.java =================================================================== --- vault-core/src/main/java/org/apache/jackrabbit/vault/fs/api/RegexpPathMapping.java (nonexistent) +++ vault-core/src/main/java/org/apache/jackrabbit/vault/fs/api/RegexpPathMapping.java (working copy) @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.jackrabbit.vault.fs.api; + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.apache.jackrabbit.vault.fs.api.PathMapping; + +/** + * Implements a path mapping that supports regular expressions, i.e. /etc/(.*)=/dummy/$1/custom + * @since 3.1.42 + */ +public final class RegexpPathMapping implements PathMapping { + + private final Map pathsMapping = new HashMap(); + + /** + * Allows importing mappings specified in data structure such as Map or Properties. + * + * All null entries (both keys and values) are ignored. + * + * @param pathsMappingMap the data structure containing the mapping + */ + public void addAllMappings(Map pathsMappingMap) { + for (Entry entry : pathsMappingMap.entrySet()) { + Object key = entry.getKey(); + Object value = entry.getValue(); + if (key != null && value != null) { + addMapping(String.valueOf(key), String.valueOf(value)); + } + } + } + + /** + * Merges the regexp mapping from the given base mapping. + * + * @param base base mapping + * @return this + */ + public RegexpPathMapping merge(RegexpPathMapping base) { + if (base != null) { + this.pathsMapping.putAll(base.pathsMapping); + } + return this; + } + + /** + * Add a new mapping based on regular expression. + * + * @param fromPattern the matching pattern, i.e. /etc/(.*) + * @param toPattern the replacing pattern, i.e. /dummy/$1/custom + * @return this + */ + public RegexpPathMapping addMapping(String fromPattern, String toPattern) { + if (fromPattern == null) { + throw new IllegalArgumentException("'fromPattern' arguments can not be 'null'"); + } + if (toPattern == null) { + throw new IllegalArgumentException("'toPattern' arguments can not be 'null'"); + } + pathsMapping.put(Pattern.compile(fromPattern), toPattern); + return this; + } + + /** + * {@inheritDoc} + */ + @Override + public String map(String path) { + for (Entry pathMapping : pathsMapping.entrySet()) { + Matcher matcher = pathMapping.getKey().matcher(path); + if (matcher.matches()) { + return matcher.replaceAll(pathMapping.getValue()); + } + } + return path; + } + + /** + * {@inheritDoc} + */ + @Override + public String map(String path, boolean reverse) { + if (reverse) { + // path mapping cannot be reversed with the regexp mapping + return path; + } + return map(path); + } + +} Property changes on: vault-core/src/main/java/org/apache/jackrabbit/vault/fs/api/RegexpPathMapping.java ___________________________________________________________________ Added: svn:eol-style ## -0,0 +1 ## +native \ No newline at end of property Added: svn:keywords ## -0,0 +1 ## +Date Author Id Revision HeadURL \ No newline at end of property Added: svn:mime-type ## -0,0 +1 ## +text/plain \ No newline at end of property Index: vault-core/src/test/java/org/apache/jackrabbit/vault/fs/api/PathMappingTest.java =================================================================== --- vault-core/src/test/java/org/apache/jackrabbit/vault/fs/api/PathMappingTest.java (revision 1802934) +++ vault-core/src/test/java/org/apache/jackrabbit/vault/fs/api/PathMappingTest.java (working copy) @@ -16,6 +16,8 @@ */ package org.apache.jackrabbit.vault.fs.api; +import static org.junit.Assert.assertEquals; + import org.junit.Test; import static junit.framework.Assert.assertEquals; @@ -77,4 +79,19 @@ assertEquals("/dest/foo/1/top/flop", map.map("/test/flop", true)); } + + @Test + public void testRegexpIdentityMapping() { + RegexpPathMapping pathMapping = new RegexpPathMapping(); + assertEquals("/etc/my/fake/data", pathMapping.map("/etc/my/fake/data")); + } + + @Test + public void testRegexpCorrectMapping() { + RegexpPathMapping pathMapping = new RegexpPathMapping(); + pathMapping.addMapping("/etc/(.*)", "/dummy/$1/custom"); + + assertEquals("/dummy/my/fake/data/custom", pathMapping.map("/etc/my/fake/data")); + } + } \ No newline at end of file Index: vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/TestPackageInstall.java =================================================================== --- vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/TestPackageInstall.java (revision 1802934) +++ vault-core/src/test/java/org/apache/jackrabbit/vault/packaging/integration/TestPackageInstall.java (working copy) @@ -27,6 +27,7 @@ import javax.jcr.nodetype.NodeType; import org.apache.commons.io.FileUtils; +import org.apache.jackrabbit.vault.fs.api.RegexpPathMapping; import org.apache.jackrabbit.vault.fs.io.ImportOptions; import org.apache.jackrabbit.vault.packaging.InstallContext; import org.apache.jackrabbit.vault.packaging.JcrPackage; @@ -82,7 +83,7 @@ */ @Test public void testUnwrapPreserveInstall() throws RepositoryException, IOException, PackageException { - + JcrPackage pack = packMgr.upload(getStream("testpackages/tmp.zip"), true, true); assertNotNull(pack); assertTrue(pack.isValid()); @@ -106,7 +107,7 @@ assertTrue(pack.isValid()); assertTrue(pack.isInstalled()); assertEquals(lastUnpacked, pack.getDefinition().getLastUnpacked().getTimeInMillis()); - + // a package with a different created date should not preserve the status! pack = packMgr.upload(getStream("testpackages/tmp_with_modified_created_date.zip"), true, true); assertNotNull(pack); @@ -580,4 +581,20 @@ // todo: upload with version // todo: rename + @Test + public void testInstallWithRemap() throws RepositoryException, IOException, PackageException { + JcrPackage pack = packMgr.upload(getStream("testpackages/test_version.zip"), false); + assertNotNull(pack); + + ImportOptions opts = getDefaultOptions(); + RegexpPathMapping pathMapping = new RegexpPathMapping(); + pathMapping.addMapping("/testroot/(.*)", "/root/$1"); + opts.setPathMapping(pathMapping); + + pack.install(opts); + + assertNodeExists("/root/a"); + assertNodeMissing("/testroot/a"); + } + } \ No newline at end of file