001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.xml.xlink;
018:
019: import java.util.HashMap;
020: import java.util.HashSet;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import org.xml.sax.Attributes;
025: import org.xml.sax.SAXException;
026: import org.xml.sax.helpers.AttributesImpl;
027:
028: /**
029: * This class extends the XLink semantic capabilities to understand those
030: * elements that are have default linking semantics associated.
031: *
032: * <p>This class reacts on 'href' and 'src' attributes and is able to understand
033: * the semantics of XHTML/WML/SMIL/SVG and all the rest of the languages that
034: * use either XLink of the above attributes.</p>
035: *
036: * <p>NOTE: this class is clearly a hack and is not future compatible, but
037: * since many XML formats to date are not compatible with the XLink semantics
038: * this is what we have to do to live in the bleeding edge. Once there will
039: * be a way to remove this, that will be a happy day for XML and for Cocoon too.</p>
040: *
041: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
042: * @author <a href="mailto:tk-cocoon@datas-world.de">Torsten Knodt</a>
043: * @version CVS $Id: ExtendedXLinkPipe.java 433543 2006-08-22 06:22:54Z crossley $
044: */
045: public abstract class ExtendedXLinkPipe extends XLinkPipe {
046:
047: protected static Set arrayToSet(Object[] array) {
048: final Set set = new HashSet(array.length);
049:
050: for (int i = 0; i < array.length; i++)
051: set.add(array[i]);
052: return set;
053: }
054:
055: private final Map MAP = new HashMap() {
056: {
057: put("", arrayToSet(new String[] { "about", "action",
058: "background", "data", "discuri", "href",
059: "longdesc", "onenterforward", "onenterbackward",
060: "ontimer", "onpick", "src" }));
061: put("http://www.w3.org/1999/xhtml",
062: arrayToSet(new String[] { "action", "background",
063: "data", "href", "longdesc", "src" }));
064: put("http://www.w3.org/2001/XInclude",
065: arrayToSet(new String[] { "href" }));
066: put("http://www.wapforum.org/2001/wml",
067: arrayToSet(new String[] { "onenterforward",
068: "onenterbackward", "ontimer", "href",
069: "onpick", "src" }));
070: put("http://www.w3.org/2002/01/P3Pv1",
071: arrayToSet(new String[] { "about", "discuri",
072: "src", "service" }));
073: }
074: };
075:
076: private int attrIndex = -1;
077:
078: public void startElement(String uri, final String name,
079: final String raw, final Attributes attr)
080: throws SAXException {
081: final Set attrList = (Set) MAP.get((uri == null) ? "" : uri);
082:
083: if (attrList != null) {
084: for (int i = attrIndex + 1; i < attr.getLength(); i++)
085: if (attr.getURI(i).length() == 0
086: && attrList.contains(attr.getLocalName(i))) {
087:
088: final String att = attr.getValue(i);
089:
090: if (att != null) {
091: final String str = ": URI=" + uri + " NAME="
092: + name + " RAW=" + raw + " ATT="
093: + attr.getLocalName(i) + " NS=" + uri
094: + " VALUE=" + att;
095:
096: if (getLogger().isDebugEnabled())
097: getLogger().debug(
098: "Transforming to XLink" + str);
099:
100: attrIndex = i;
101:
102: simpleLink(att, null, null, null, null, null,
103: uri, name, raw, attr);
104:
105: return;
106: }
107: }
108: attrIndex = -1;
109: }
110:
111: super .startElement(uri, name, raw, attr);
112: }
113:
114: public void simpleLink(final String href, final String role,
115: final String arcrole, final String title,
116: final String show, final String actuate, final String uri,
117: final String name, final String raw, final Attributes attr)
118: throws SAXException {
119: if (attrIndex != -1) {
120: final AttributesImpl newattr = new AttributesImpl(attr);
121: newattr.setValue(attrIndex, href);
122: startElement(uri, name, raw, newattr);
123: } else {
124: super.simpleLink(href, role, arcrole, title, show, actuate,
125: uri, name, raw, attr);
126: }
127: }
128: }
|