Author: cziegeler
Date: Mon Sep 1 23:42:32 2008
New Revision: 691122
URL: http://svn.apache.org/viewvc?rev=691122&view=rev
Log:
SLING-637 Add regexp for resource resolution. All configured regexp are applied
before the mapping is tested.
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java
incubator/sling/trunk/jcr/resource/src/main/resources/OSGI-INF/metatype/metatype.properties
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java?rev=691122&r1=691121&r2=691122&view=diff
==============================================================================
---
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
(original)
+++
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolver.java
Mon Sep 1 23:42:32 2008
@@ -21,6 +21,7 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
+import java.util.regex.Matcher;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
@@ -38,6 +39,7 @@
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.jcr.resource.JcrResourceUtil;
+import
org.apache.sling.jcr.resource.internal.JcrResourceResolverFactoryImpl.ResourcePattern;
import org.apache.sling.jcr.resource.internal.helper.Mapping;
import org.apache.sling.jcr.resource.internal.helper.ResourcePathIterator;
import
org.apache.sling.jcr.resource.internal.helper.jcr.JcrNodeResourceIterator;
@@ -133,6 +135,9 @@
href = resourcePath;
}
+ // apply regexp
+ href = applyPattern(href, factory.getBackPatterns());
+
// check virtual mappings
String virtual = factory.realToVirtualUri(href);
if (virtual != null) {
@@ -256,7 +261,10 @@
}
private Resource urlToResource(String uri)
- throws SlingException {
+ throws SlingException {
+ // apply regexp
+ uri = applyPattern(uri, factory.getPatterns());
+
Mapping[] mappings = factory.getMappings();
for (int i = 0; i < mappings.length; i++) {
// exchange the 'to'-portion with the 'from' portion and check
@@ -287,6 +295,27 @@
}
+ private String applyPattern(String uri, final ResourcePattern[] patterns) {
+ for(final ResourcePattern pattern : patterns) {
+ final Matcher matcher = pattern.pattern.matcher(uri);
+
+ final StringBuffer myStringBuffer = new StringBuffer();
+ while (matcher.find()) {
+ String repl = pattern.replacement;
+ for(int i=1; i<=matcher.groupCount(); i++) {
+ int pos = repl.indexOf("$"+i);
+ if ( pos != -1 ) {
+ repl = repl.substring(0, pos) + matcher.group(i) +
repl.substring(pos +2);
+ }
+ }
+ matcher.appendReplacement(myStringBuffer, repl);
+ }
+ matcher.appendTail(myStringBuffer);
+ uri = myStringBuffer.toString();
+ }
+ return uri;
+ }
+
private Resource scanPath(final String uriPath)
throws SlingException {
Resource resource = null;
@@ -309,7 +338,7 @@
String rpi = uriPath.substring(curPath.length());
resource.getResourceMetadata().setResolutionPathInfo(rpi);
}
-
+
return resource;
}
Modified:
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java?rev=691122&r1=691121&r2=691122&view=diff
==============================================================================
---
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java
(original)
+++
incubator/sling/trunk/jcr/resource/src/main/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverFactoryImpl.java
Mon Sep 1 23:42:32 2008
@@ -25,6 +25,7 @@
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
+import java.util.regex.Pattern;
import javax.jcr.Session;
@@ -70,8 +71,18 @@
* cardinality="0..n" policy="dynamic"
*/
-public class JcrResourceResolverFactoryImpl implements
- JcrResourceResolverFactory {
+public class JcrResourceResolverFactoryImpl
+ implements JcrResourceResolverFactory {
+
+ public final static class ResourcePattern {
+ public final Pattern pattern;
+ public final String replacement;
+
+ public ResourcePattern(final Pattern p, final String r) {
+ this.pattern = p;
+ this.replacement = r;
+ }
+ }
/**
* @scr.property value="true" type="Boolean"
@@ -101,6 +112,21 @@
*/
public static final String PROP_PATH = "resource.resolver.searchpath";
+ /**
+ * These regexps are executing during the resource resolving phase
+ * before the mappings are applied.
+ * @scr.property values.1="/_(.+?)_|/$1:"
+ */
+ private static final String PROP_REGEXPS = "resource.resolver.regexps";
+
+ /**
+ * These regexps are executed during a map operation as the back conversion
+ * of the [EMAIL PROTECTED] #PROP_REGEXPS}.
+ * @scr.property values.1="/_(.+?)_|/$1:"
+ */
+ private static final String PROP_MAPREGEXPS =
"resource.resolver.mapregexps";
+
+
/** default log */
private final Logger log = LoggerFactory.getLogger(getClass());
@@ -135,6 +161,12 @@
/** The fake urls */
private BidiMap virtualURLMap;
+ /** The regexp patterns */
+ private ResourcePattern[] patterns;
+
+ /** The back regexp patterns */
+ private ResourcePattern[] backPatterns;
+
/** <code>true</code>, if direct mappings from URI to handle are allowed */
private boolean allowDirect = false;
@@ -201,6 +233,14 @@
return searchPath;
}
+ ResourcePattern[] getPatterns() {
+ return patterns;
+ }
+
+ ResourcePattern[] getBackPatterns() {
+ return backPatterns;
+ }
+
// ---------- SCR Integration ---------------------------------------------
/** Activates this component, called by SCR before registering as a
service */
@@ -236,6 +276,10 @@
mappings = tmp;
}
+ // regexps
+ this.patterns = this.getResourcePatterns((String[])
properties.get(PROP_REGEXPS));
+ this.backPatterns = this.getResourcePatterns((String[])
properties.get(PROP_MAPREGEXPS));
+
// from configuration if available
searchPath = OsgiUtil.toStringArray(properties.get(PROP_PATH));
if (searchPath != null && searchPath.length > 0) {
@@ -262,6 +306,24 @@
this.processDelayedJcrResourceTypeProviders();
}
+ private ResourcePattern[] getResourcePatterns(String[] patternList) {
+ // regexps
+ List<ResourcePattern> patterns = new ArrayList<ResourcePattern>();
+ if ( patternList != null ) {
+ for(final String p : patternList) {
+ int pos = p.lastIndexOf('|');
+ if ( pos == -1 ) {
+ log.error("Invalid regexp: {}", p);
+ } else {
+ final String replString = p.substring(pos + 1);
+ final Pattern pat = Pattern.compile(p.substring(0, pos));
+ patterns.add(new ResourcePattern(pat, replString));
+ }
+ }
+ }
+ return patterns.toArray(new ResourcePattern[patterns.size()]);
+ }
+
protected void processDelayedJcrResourceTypeProviders() {
synchronized ( this.jcrResourceTypeProviders ) {
for(ServiceReference reference : delayedJcrResourceTypeProviders )
{
Modified:
incubator/sling/trunk/jcr/resource/src/main/resources/OSGI-INF/metatype/metatype.properties
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/main/resources/OSGI-INF/metatype/metatype.properties?rev=691122&r1=691121&r2=691122&view=diff
==============================================================================
---
incubator/sling/trunk/jcr/resource/src/main/resources/OSGI-INF/metatype/metatype.properties
(original)
+++
incubator/sling/trunk/jcr/resource/src/main/resources/OSGI-INF/metatype/metatype.properties
Mon Sep 1 23:42:32 2008
@@ -48,3 +48,12 @@
applied to find resources whose path is just specified with a relative path. \
The default value is [ "/apps", "/libs" ]. If an empty path is specified a \
single entry path of [ "/" ] is assumed.
+resource.resolver.regexps.name = Resource Regexps
+resource.resolver.regexps.description = The list of regexps that will be \
+ executed on a path before the resource resolving. Please separate the \
+ regexp from the replacement string by using the | (pipe) character. \
+ For each regexp you need to specify a regexp doing the opposite conversion.
+resource.resolver.mapregexps.name = Map Regexps
+resource.resolver.mapregexps.description = The list of regexps that will do \
+ the opposite conversion as the resource regexps.
+
\ No newline at end of file
Modified:
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java
URL:
http://svn.apache.org/viewvc/incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java?rev=691122&r1=691121&r2=691122&view=diff
==============================================================================
---
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java
(original)
+++
incubator/sling/trunk/jcr/resource/src/test/java/org/apache/sling/jcr/resource/internal/JcrResourceResolverTest.java
Mon Sep 1 23:42:32 2008
@@ -65,6 +65,10 @@
mappingsField.setAccessible(true);
mappingsField.set(resFac, new Mapping[] { Mapping.DIRECT });
+ Field patternsField = resFac.getClass().getDeclaredField("patterns");
+ patternsField.setAccessible(true);
+ patternsField.set(resFac, new
JcrResourceResolverFactoryImpl.ResourcePattern[0]);
+
try {
NamespaceRegistry nsr =
session.getWorkspace().getNamespaceRegistry();
nsr.registerNamespace(SlingConstants.NAMESPACE_PREFIX,