You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by an...@apache.org on 2007/06/12 13:49:19 UTC
svn commit: r546459 - in /lenya/trunk/src/modules-core/linking:
config/sitemap/ java/src/org/apache/lenya/cms/cocoon/transformation/
Author: andreas
Date: Tue Jun 12 04:49:18 2007
New Revision: 546459
URL: http://svn.apache.org/viewvc?view=rev&rev=546459
Log:
Refactoring: common base class for link transformers
Added:
lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/AbstractLinkTransformer.java
Modified:
lenya/trunk/src/modules-core/linking/config/sitemap/transformers.xmap
lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UrlToUuidTransformer.java
lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java
Modified: lenya/trunk/src/modules-core/linking/config/sitemap/transformers.xmap
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/config/sitemap/transformers.xmap?view=diff&rev=546459&r1=546458&r2=546459
==============================================================================
--- lenya/trunk/src/modules-core/linking/config/sitemap/transformers.xmap (original)
+++ lenya/trunk/src/modules-core/linking/config/sitemap/transformers.xmap Tue Jun 12 04:49:18 2007
@@ -29,6 +29,11 @@
</map:transformer>
<map:transformer name="url2uuid" logger="lenya.sitemap.transformer.url2uuid"
- src="org.apache.lenya.cms.cocoon.transformation.UrlToUuidTransformer"/>
+ src="org.apache.lenya.cms.cocoon.transformation.UrlToUuidTransformer">
+ <transform namespace="http://www.w3.org/1999/xhtml" element="a" attribute="href"/>
+ <transform namespace="http://www.w3.org/1999/xhtml" element="object" attribute="data"/>
+ <transform namespace="http://www.w3.org/1999/xhtml" element="img" attribute="src"/>
+ <transform namespace="http://www.w3.org/1999/xhtml" element="link" attribute="href"/>
+ </map:transformer>
</xmap>
Added: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/AbstractLinkTransformer.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/AbstractLinkTransformer.java?view=auto&rev=546459
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/AbstractLinkTransformer.java (added)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/AbstractLinkTransformer.java Tue Jun 12 04:49:18 2007
@@ -0,0 +1,208 @@
+/*
+ * 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.lenya.cms.cocoon.transformation;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
+import org.apache.cocoon.transformation.AbstractSAXTransformer;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.AttributesImpl;
+
+/**
+ * <p>
+ * This transformer processes all links which are configured using
+ * <code><transform/></code> elements:
+ * </p>
+ * <code><pre>
+ * <map:transformer ... >
+ * <transform namespace="http://www.w3.org/1999/xhtml" element="a" attribute="href"/>
+ * <transform namespace="..." ... />
+ * </map:transformer>
+ * </pre></code>
+ */
+public abstract class AbstractLinkTransformer extends AbstractSAXTransformer {
+
+ private List attrConfigs = new ArrayList();
+
+ protected AttributeConfiguration[] getAttributeConfigurations() {
+ return (AttributeConfiguration[]) this.attrConfigs
+ .toArray(new AttributeConfiguration[this.attrConfigs.size()]);
+ }
+
+ public void configure(Configuration config) throws ConfigurationException {
+ super.configure(config);
+ Configuration[] transformConfigs = config.getChildren("transform");
+ for (int i = 0; i < transformConfigs.length; i++) {
+ String namespace = transformConfigs[i].getAttribute("namespace");
+ String element = transformConfigs[i].getAttribute("element");
+ String attribute = transformConfigs[i].getAttribute("attribute");
+ AttributeConfiguration attrConfig = new AttributeConfiguration(namespace, element,
+ attribute);
+ this.attrConfigs.add(attrConfig);
+ }
+ }
+
+ /**
+ * Declaration of an attribute which should be transformed.
+ */
+ public static class AttributeConfiguration {
+
+ protected final String element;
+ protected final String namespace;
+ protected final String attribute;
+
+ /**
+ * @param namespace The namespace of the element.
+ * @param element The local name of the element.
+ * @param attribute The name of the attribute to transform.
+ */
+ public AttributeConfiguration(String namespace, String element, String attribute) {
+ this.namespace = namespace;
+ this.element = element;
+ this.attribute = attribute;
+ }
+
+ /**
+ * @param uri The namespace URI.
+ * @param name The local name.
+ * @param attrs The attributes.
+ * @return If this configuration matches the parameters.
+ */
+ public boolean matches(String uri, String name, Attributes attrs) {
+ return this.namespace.equals(uri) && this.element.equals(name)
+ && attrs.getValue(this.attribute) != null;
+ }
+
+ }
+
+ protected AttributeConfiguration[] getMatchingConfigurations(String namespace,
+ String localName, Attributes attrs) {
+ List configs = new ArrayList();
+ for (Iterator i = this.attrConfigs.iterator(); i.hasNext();) {
+ AttributeConfiguration config = (AttributeConfiguration) i.next();
+ if (config.matches(namespace, localName, attrs)) {
+ configs.add(config);
+ }
+ }
+ return (AttributeConfiguration[]) configs
+ .toArray(new AttributeConfiguration[configs.size()]);
+ }
+
+ protected boolean existsMatchingConfiguration(String namespace, String localName) {
+ boolean matches = false;
+ AttributeConfiguration[] attrConfigs = getAttributeConfigurations();
+ for (int i = 0; i < attrConfigs.length; i++) {
+ if (attrConfigs[i].namespace.equals(namespace)
+ && attrConfigs[i].element.equals(localName)) {
+ matches = true;
+ }
+ }
+ return matches;
+ }
+
+ protected String indent = "";
+ protected boolean ignoreLinkElement = false;
+
+ /**
+ * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
+ * java.lang.String, java.lang.String, org.xml.sax.Attributes)
+ */
+ public void startElement(String uri, String name, String qname, Attributes attrs)
+ throws SAXException {
+
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug(
+ this.indent + "<" + qname + "> (ignoreAElement = " + this.ignoreLinkElement
+ + ")");
+ this.indent += " ";
+ }
+
+ AttributesImpl newAttrs = null;
+
+ AttributeConfiguration[] configs = getMatchingConfigurations(uri, name, attrs);
+
+ if (configs.length > 0) {
+
+ this.ignoreLinkElement = false;
+
+ for (int i = 0; i < configs.length; i++) {
+ AttributeConfiguration config = configs[i];
+ String linkUrl = attrs.getValue(config.attribute);
+ try {
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug(this.indent + "link URL: [" + linkUrl + "]");
+ }
+ newAttrs = new AttributesImpl(attrs);
+ handleLink(linkUrl, config, newAttrs);
+ } catch (final Exception e) {
+ getLogger().error("startElement failed: ", e);
+ throw new SAXException(e);
+ }
+ }
+ }
+
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug(this.indent + "ignoreAElement: " + this.ignoreLinkElement);
+ }
+
+ if (!(configs.length > 0 && this.ignoreLinkElement)) {
+ if (newAttrs != null) {
+ attrs = newAttrs;
+ }
+ super.startElement(uri, name, qname, attrs);
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug(this.indent + "<" + qname + "> sent");
+ }
+ }
+ }
+
+ /**
+ * Handle a link in the source SAX stream.
+ * @param linkUrl The link URL.
+ * @param config The attribute configuration which matched the link.
+ * @param newAttrs The new attributes which will be added to the result element.
+ * @throws Exception if an error occurs.
+ */
+ protected abstract void handleLink(String linkUrl, AttributeConfiguration config,
+ AttributesImpl newAttrs) throws Exception;
+
+ /**
+ * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
+ * java.lang.String, java.lang.String)
+ */
+ public void endElement(String uri, String name, String qname) throws SAXException {
+ if (getLogger().isDebugEnabled()) {
+ this.indent = this.indent.substring(2);
+ getLogger().debug(this.indent + "</" + qname + ">");
+ }
+ if (existsMatchingConfiguration(uri, name) && this.ignoreLinkElement) {
+ this.ignoreLinkElement = false;
+ } else {
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug(this.indent + "</" + qname + "> sent");
+ }
+ super.endElement(uri, name, qname);
+ }
+ }
+
+}
Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UrlToUuidTransformer.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UrlToUuidTransformer.java?view=diff&rev=546459&r1=546458&r2=546459
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UrlToUuidTransformer.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UrlToUuidTransformer.java Tue Jun 12 04:49:18 2007
@@ -18,7 +18,6 @@
package org.apache.lenya.cms.cocoon.transformation;
import java.io.IOException;
-import java.util.Arrays;
import java.util.Map;
import org.apache.avalon.framework.parameters.Parameters;
@@ -26,7 +25,6 @@
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;
-import org.apache.cocoon.transformation.AbstractSAXTransformer;
import org.apache.lenya.ac.AccessControlException;
import org.apache.lenya.cms.linking.Link;
import org.apache.lenya.cms.linking.LinkResolver;
@@ -36,7 +34,6 @@
import org.apache.lenya.cms.repository.RepositoryUtil;
import org.apache.lenya.cms.repository.Session;
import org.apache.lenya.util.ServletHelper;
-import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
@@ -52,7 +49,7 @@
*
* $Id: LinkRewritingTransformer.java,v 1.7 2004/03/16 11:12:16 gregor
*/
-public class UrlToUuidTransformer extends AbstractSAXTransformer {
+public class UrlToUuidTransformer extends AbstractLinkTransformer {
private boolean ignoreLinkElement = false;
private Document currentDocument;
@@ -98,108 +95,6 @@
private String indent = "";
/**
- * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
- * java.lang.String, java.lang.String, org.xml.sax.Attributes)
- */
- public void startElement(String uri, String name, String qname, Attributes attrs)
- throws SAXException {
-
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(
- this.indent + "<" + qname + "> (ignoreAElement = " + this.ignoreLinkElement
- + ")");
- this.indent += " ";
- }
-
- AttributesImpl newAttrs = null;
- if (lookingAtLinkElement(name)) {
-
- this.ignoreLinkElement = false;
-
- for (int i = 0; i < UuidToUrlTransformer.attributeNames.length; i++) {
- String completeUrl = attrs.getValue(UuidToUrlTransformer.attributeNames[i]);
- if (completeUrl != null) {
- try {
- newAttrs = new AttributesImpl(attrs);
-
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(this.indent + "link URL: [" + completeUrl + "]");
- }
-
- String pubId = this.currentDocument.getPublication().getId();
- String area = this.currentDocument.getArea();
-
- if (completeUrl.startsWith(this.contextPath + "/" + pubId + "/" + area
- + "/")) {
- final String webappUrl = completeUrl.substring(this.contextPath
- .length());
-
- String anchor = null;
- String url = null;
-
- int anchorIndex = webappUrl.indexOf("#");
- if (anchorIndex > -1) {
- url = webappUrl.substring(0, anchorIndex);
- anchor = webappUrl.substring(anchorIndex + 1);
- } else {
- url = webappUrl;
- }
-
- String[] linkUrlAndQuery = url.split("\\?");
- String linkUrl = linkUrlAndQuery[0];
- String queryString = null;
- if (linkUrlAndQuery.length > 1) {
- queryString = linkUrlAndQuery[1];
- }
-
- if (factory.isDocument(linkUrl)) {
- Document targetDocument = factory.getFromURL(linkUrl);
-
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(
- "Convert links: Check webapp URL [" + linkUrl + "]");
- }
-
- rewriteLink(newAttrs, UuidToUrlTransformer.attributeNames[i],
- targetDocument, anchor, queryString);
- }
- else {
- /*
- * This is legacy code. It rewrites links to
- * non-document images (in resources/shared). These
- * images shouldn't be referenced in documents since
- * this violates the separation between content and
- * layout.
- */
- String newUrl = completeUrl.substring(this.contextPath.length());
- setAttribute(newAttrs, UuidToUrlTransformer.attributeNames[i], newUrl);
- }
- }
-
- } catch (final Exception e) {
- getLogger().error("startElement failed: ", e);
- throw new SAXException(e);
- }
- }
- }
- }
-
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(this.indent + "ignoreAElement: " + this.ignoreLinkElement);
- }
-
- if (!(lookingAtLinkElement(name) && this.ignoreLinkElement)) {
- if (newAttrs != null) {
- attrs = newAttrs;
- }
- super.startElement(uri, name, qname, attrs);
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(this.indent + "<" + qname + "> sent");
- }
- }
- }
-
- /**
* Rewrites a link.
*
* @param newAttrs The new attributes.
@@ -251,32 +146,57 @@
}
/**
- * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
- * java.lang.String, java.lang.String)
- */
- public void endElement(String uri, String name, String qname) throws SAXException {
- if (getLogger().isDebugEnabled()) {
- this.indent = this.indent.substring(2);
- getLogger().debug(this.indent + "</" + qname + ">");
- }
- if (lookingAtLinkElement(name) && this.ignoreLinkElement) {
- this.ignoreLinkElement = false;
- } else {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(this.indent + "</" + qname + "> sent");
- }
- super.endElement(uri, name, qname);
- }
- }
-
- private boolean lookingAtLinkElement(String name) {
- return Arrays.asList(UuidToUrlTransformer.elementNames).contains(name);
- }
-
- /**
* @see org.apache.avalon.excalibur.pool.Recyclable#recycle()
*/
public void recycle() {
this.ignoreLinkElement = false;
+ }
+
+ protected void handleLink(String originalUrl, AttributeConfiguration config,
+ AttributesImpl newAttrs) throws Exception {
+
+ String pubId = this.currentDocument.getPublication().getId();
+ String area = this.currentDocument.getArea();
+
+ if (originalUrl.startsWith(this.contextPath + "/" + pubId + "/" + area + "/")) {
+ final String webappUrl = originalUrl.substring(this.contextPath.length());
+
+ String anchor = null;
+ String url = null;
+
+ int anchorIndex = webappUrl.indexOf("#");
+ if (anchorIndex > -1) {
+ url = webappUrl.substring(0, anchorIndex);
+ anchor = webappUrl.substring(anchorIndex + 1);
+ } else {
+ url = webappUrl;
+ }
+
+ String[] linkUrlAndQuery = url.split("\\?");
+ String linkUrl = linkUrlAndQuery[0];
+ String queryString = null;
+ if (linkUrlAndQuery.length > 1) {
+ queryString = linkUrlAndQuery[1];
+ }
+
+ if (factory.isDocument(linkUrl)) {
+ Document targetDocument = factory.getFromURL(linkUrl);
+
+ if (getLogger().isDebugEnabled()) {
+ getLogger().debug("Convert links: Check webapp URL [" + linkUrl + "]");
+ }
+
+ rewriteLink(newAttrs, config.attribute, targetDocument, anchor, queryString);
+ } else {
+ /*
+ * This is legacy code. It rewrites links to non-document images
+ * (in resources/shared). These images shouldn't be referenced
+ * in documents since this violates the separation between
+ * content and layout.
+ */
+ String newUrl = originalUrl.substring(this.contextPath.length());
+ setAttribute(newAttrs, config.attribute, newUrl);
+ }
+ }
}
}
Modified: lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java
URL: http://svn.apache.org/viewvc/lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java?view=diff&rev=546459&r1=546458&r2=546459
==============================================================================
--- lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java (original)
+++ lenya/trunk/src/modules-core/linking/java/src/org/apache/lenya/cms/cocoon/transformation/UuidToUrlTransformer.java Tue Jun 12 04:49:18 2007
@@ -20,7 +20,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Iterator;
import java.util.List;
import java.util.Map;
@@ -34,7 +33,6 @@
import org.apache.cocoon.environment.ObjectModelHelper;
import org.apache.cocoon.environment.Request;
import org.apache.cocoon.environment.SourceResolver;
-import org.apache.cocoon.transformation.AbstractSAXTransformer;
import org.apache.lenya.ac.AccessControlException;
import org.apache.lenya.ac.AccessController;
import org.apache.lenya.ac.AccessControllerResolver;
@@ -53,7 +51,6 @@
import org.apache.lenya.util.Query;
import org.apache.lenya.util.ServletHelper;
import org.apache.lenya.util.StringUtil;
-import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
@@ -90,28 +87,27 @@
* either configure this when declaring the transformer:
* </p>
* <code><pre>
- * <map:transformer ... >
- * <urls type="relative"/>
- * <transform namespace="http://www.w3.org/1999/xhtml" element="a" attribute="href"/>
- * <transform namespace="..." ... />
- * </map:transformer>
+ * <map:transformer ... >
+ * <urls type="relative"/>
+ * <transform namespace="http://www.w3.org/1999/xhtml" element="a" attribute="href"/>
+ * <transform namespace="..." ... />
+ * </map:transformer>
* </pre></code>
* <p>
* or pass a parameter:
* </p>
* <code><pre>
- * <map:parameter name="urls" value="relative"/>
+ * <map:parameter name="urls" value="relative"/>
* </pre></code>
*
* $Id: LinkRewritingTransformer.java,v 1.7 2004/03/16 11:12:16 gregor
*/
-public class UuidToUrlTransformer extends AbstractSAXTransformer implements Disposable {
+public class UuidToUrlTransformer extends AbstractLinkTransformer implements Disposable {
protected static final String BROKEN_ATTRIB = "class";
protected static final String BROKEN_VALUE = "brokenlink";
protected static final String EXTENSION_PARAM = "uuid2url.extension";
- private boolean ignoreLinkElement = false;
private ServiceSelector serviceSelector;
private PolicyManager policyManager;
private AccessControllerResolver acResolver;
@@ -192,50 +188,6 @@
String value = urlConfig.getAttribute("type");
setUrlType(value);
}
- Configuration[] transformConfigs = config.getChildren("transform");
- for (int i = 0; i < transformConfigs.length; i++) {
- String namespace = transformConfigs[i].getAttribute("namespace");
- String element = transformConfigs[i].getAttribute("element");
- String attribute = transformConfigs[i].getAttribute("attribute");
- AttributeConfiguration attrConfig = new AttributeConfiguration(namespace, element,
- attribute);
- this.attributeConfigurations.add(attrConfig);
- }
- }
-
- private List attributeConfigurations = new ArrayList();
-
- /**
- * Declaration of an attribute which should be transformed.
- */
- public static class AttributeConfiguration {
-
- protected final String element;
- protected final String namespace;
- protected final String attribute;
-
- /**
- * @param namespace The namespace of the element.
- * @param element The local name of the element.
- * @param attribute The name of the attribute to transform.
- */
- public AttributeConfiguration(String namespace, String element, String attribute) {
- this.namespace = namespace;
- this.element = element;
- this.attribute = attribute;
- }
-
- /**
- * @param uri The namespace URI.
- * @param name The local name.
- * @param attrs The attributes.
- * @return If this configuration matches the parameters.
- */
- public boolean matches(String uri, String name, Attributes attrs) {
- return this.namespace.equals(uri) && this.element.equals(name)
- && attrs.getValue(this.attribute) != null;
- }
-
}
protected void setUrlType(String value) throws ConfigurationException {
@@ -258,135 +210,65 @@
return this.currentDocument;
}
- private String indent = "";
-
- protected static final String[] elementNames = { "a", "object", "img", "link" };
- protected static final String[] attributeNames = { "href", "src", "data" };
-
- protected AttributeConfiguration[] getMatchingConfigurations(String namespace,
- String localName, Attributes attrs) {
- List configs = new ArrayList();
- for (Iterator i = this.attributeConfigurations.iterator(); i.hasNext();) {
- AttributeConfiguration config = (AttributeConfiguration) i.next();
- if (config.matches(namespace, localName, attrs)) {
- configs.add(config);
+ protected void handleLink(String linkUrl, AttributeConfiguration config, AttributesImpl newAttrs)
+ throws Exception {
+ if (linkUrl.startsWith("lenya-document:")) {
+ Document doc = getCurrentDocument();
+
+ String anchor = null;
+ String url = null;
+
+ int anchorIndex = linkUrl.indexOf("#");
+ if (anchorIndex > -1) {
+ url = linkUrl.substring(0, anchorIndex);
+ anchor = linkUrl.substring(anchorIndex + 1);
+ } else {
+ url = linkUrl;
}
- }
- return (AttributeConfiguration[]) configs
- .toArray(new AttributeConfiguration[configs.size()]);
- }
-
- /**
- * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
- * java.lang.String, java.lang.String, org.xml.sax.Attributes)
- */
- public void startElement(String uri, String name, String qname, Attributes attrs)
- throws SAXException {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(
- this.indent + "<" + qname + "> (ignoreAElement = " + this.ignoreLinkElement
- + ")");
- this.indent += " ";
- }
-
- AttributesImpl newAttrs = null;
-
- AttributeConfiguration[] configs = getMatchingConfigurations(uri, name, attrs);
-
- if (configs.length > 0) {
-
- this.ignoreLinkElement = false;
-
- for (int i = 0; i < configs.length; i++) {
- AttributeConfiguration config = configs[i];
- String linkUrl = attrs.getValue(config.attribute);
- try {
- newAttrs = new AttributesImpl(attrs);
-
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(this.indent + "link URL: [" + linkUrl + "]");
- }
-
- if (linkUrl.startsWith("lenya-document:")) {
- Document doc = getCurrentDocument();
-
- String anchor = null;
- String url = null;
-
- int anchorIndex = linkUrl.indexOf("#");
- if (anchorIndex > -1) {
- url = linkUrl.substring(0, anchorIndex);
- anchor = linkUrl.substring(anchorIndex + 1);
- } else {
- url = linkUrl;
- }
-
- String[] linkUriAndQuery = url.split("\\?");
- String linkUri = linkUriAndQuery[0];
- String queryString = null;
- String requiredExtension = null;
- if (linkUriAndQuery.length > 1) {
- queryString = linkUriAndQuery[1];
- Query query = new Query(queryString);
- requiredExtension = query.getValue(EXTENSION_PARAM);
- query.removeValue(EXTENSION_PARAM);
- queryString = query.toString();
- }
- LinkTarget target = this.linkResolver.resolve(doc, linkUri);
- if (target.exists() && target.getDocument().hasLink()) {
- Document targetDocument = target.getDocument();
-
- String extension = getExtension(targetDocument, requiredExtension);
-
- rewriteLink(newAttrs, config.attribute, targetDocument, anchor,
- queryString, extension);
- } else if (doc.getArea().equals(Publication.AUTHORING_AREA)) {
- markBrokenLink(newAttrs, config.attribute, linkUrl);
- } else {
- this.ignoreLinkElement = true;
- }
- } else {
- /*
- * This is legacy code. It rewrites links to
- * non-document images (in resources/shared). These
- * images shouldn't be referenced in documents since
- * this violates the separation between content and
- * layout.
- */
- String prefix = "/" + this.currentDocument.getPublication().getId() + "/";
- if (linkUrl.startsWith(prefix)) {
- String pubUrl = linkUrl.substring(prefix.length());
- String area = pubUrl.split("/")[0];
-
- // don't rewrite /{pub}/modules/...
- if (area.equals(Publication.AUTHORING_AREA)) {
- String areaUrl = pubUrl.substring(area.length());
- String newUrl = this.contextPath + prefix
- + this.currentDocument.getArea() + areaUrl;
- setAttribute(newAttrs, config.attribute, newUrl);
- }
- }
- }
-
- } catch (final Exception e) {
- getLogger().error("startElement failed: ", e);
- throw new SAXException(e);
- }
- }
- }
-
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(this.indent + "ignoreAElement: " + this.ignoreLinkElement);
- }
-
- if (!(configs.length > 0 && this.ignoreLinkElement)) {
- if (newAttrs != null) {
- attrs = newAttrs;
+ String[] linkUriAndQuery = url.split("\\?");
+ String linkUri = linkUriAndQuery[0];
+ String queryString = null;
+ String requiredExtension = null;
+ if (linkUriAndQuery.length > 1) {
+ queryString = linkUriAndQuery[1];
+ Query query = new Query(queryString);
+ requiredExtension = query.getValue(EXTENSION_PARAM);
+ query.removeValue(EXTENSION_PARAM);
+ queryString = query.toString();
+ }
+ LinkTarget target = this.linkResolver.resolve(doc, linkUri);
+ if (target.exists() && target.getDocument().hasLink()) {
+ Document targetDocument = target.getDocument();
+
+ String extension = getExtension(targetDocument, requiredExtension);
+
+ rewriteLink(newAttrs, config.attribute, targetDocument, anchor, queryString,
+ extension);
+ } else if (doc.getArea().equals(Publication.AUTHORING_AREA)) {
+ markBrokenLink(newAttrs, config.attribute, linkUrl);
+ } else {
+ this.ignoreLinkElement = true;
}
- super.startElement(uri, name, qname, attrs);
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(this.indent + "<" + qname + "> sent");
+ } else {
+ /*
+ * This is legacy code. It rewrites links to non-document images (in
+ * resources/shared). These images shouldn't be referenced in
+ * documents since this violates the separation between content and
+ * layout.
+ */
+ String prefix = "/" + this.currentDocument.getPublication().getId() + "/";
+ if (linkUrl.startsWith(prefix)) {
+ String pubUrl = linkUrl.substring(prefix.length());
+ String area = pubUrl.split("/")[0];
+
+ // don't rewrite /{pub}/modules/...
+ if (area.equals(Publication.AUTHORING_AREA)) {
+ String areaUrl = pubUrl.substring(area.length());
+ String newUrl = this.contextPath + prefix + this.currentDocument.getArea()
+ + areaUrl;
+ setAttribute(newAttrs, config.attribute, newUrl);
+ }
}
}
}
@@ -524,32 +406,6 @@
throw new IllegalArgumentException("The href attribute is not available!");
}
attr.setValue(position, value);
- }
-
- /**
- * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
- * java.lang.String, java.lang.String)
- */
- public void endElement(String uri, String name, String qname) throws SAXException {
- if (getLogger().isDebugEnabled()) {
- this.indent = this.indent.substring(2);
- getLogger().debug(this.indent + "</" + qname + ">");
- }
- boolean matches = false;
- for (Iterator i = this.attributeConfigurations.iterator(); i.hasNext();) {
- AttributeConfiguration config = (AttributeConfiguration) i.next();
- if (config.namespace.equals(uri) && config.element.equals(name)) {
- matches = true;
- }
- }
- if (matches && this.ignoreLinkElement) {
- this.ignoreLinkElement = false;
- } else {
- if (getLogger().isDebugEnabled()) {
- getLogger().debug(this.indent + "</" + qname + "> sent");
- }
- super.endElement(uri, name, qname);
- }
}
/**
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org