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 org.apache.cocoon.xml.AbstractXMLPipe;
020: import org.xml.sax.Attributes;
021: import org.xml.sax.SAXException;
022: import org.xml.sax.helpers.AttributesImpl;
023:
024: /**
025: * This class implements a SAX consumer wrapper that transforms the
026: * general SAX semantic into XLink semantics for easier consumption.
027: *
028: * Classes should extend this class and overwrite the abstract method
029: * to consume the XLink events that come in as SAX events.
030: *
031: * NOTE: this is based on XLink W3C Candidate Recommendation 3 July 2000
032: *
033: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
034: * @version CVS $Id: XLinkPipe.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036:
037: public abstract class XLinkPipe extends AbstractXMLPipe implements
038: XLinkHandler {
039:
040: public static final String XLINK_NAMESPACE_URI = "http://www.w3.org/1999/xlink";
041: public static final String XLINK_TYPE = "type";
042: public static final String XLINK_HREF = "href";
043: public static final String XLINK_ROLE = "role";
044: public static final String XLINK_ARCROLE = "arcrole";
045: public static final String XLINK_TITLE = "title";
046: public static final String XLINK_SHOW = "show";
047: public static final String XLINK_ACTUATE = "actuate";
048: public static final String XLINK_LABEL = "label";
049: public static final String XLINK_FROM = "from";
050: public static final String XLINK_TO = "to";
051: public static final String XLINK_TYPE_SIMPLE = "simple";
052: public static final String XLINK_TYPE_EXTENDED = "extended";
053: public static final String XLINK_TYPE_LOCATOR = "locator";
054: public static final String XLINK_TYPE_ARC = "arc";
055: public static final String XLINK_TYPE_RESOURCE = "resource";
056: public static final String XLINK_TYPE_TITLE = "title";
057:
058: private String extendedLinkElementName = null;
059: private String extendedLinkElementURI = null;
060: private String linkLocatorElementName = null;
061: private String linkLocatorElementURI = null;
062: private String linkArcElementName = null;
063: private String linkArcElementURI = null;
064:
065: public void startElement(String uri, String name, String raw,
066: Attributes attr) throws SAXException {
067: String type = attr.getValue(XLINK_NAMESPACE_URI, XLINK_TYPE);
068: if (type != null) {
069: if (type.equals(XLINK_TYPE_SIMPLE)) {
070: if (this .extendedLinkElementName != null) {
071: throw new SAXException(
072: "An XLink simple link cannot be included into an 'extended' element");
073: } else if (this .linkLocatorElementName != null) {
074: throw new SAXException(
075: "An XLink simple link cannot be included into a 'locator' element");
076: } else if (this .linkArcElementName != null) {
077: throw new SAXException(
078: "An XLink simple link cannot be included into an 'arc' element");
079: }
080: String href = attr.getValue(XLINK_NAMESPACE_URI,
081: XLINK_HREF);
082: String role = attr.getValue(XLINK_NAMESPACE_URI,
083: XLINK_ROLE);
084: String arcrole = attr.getValue(XLINK_NAMESPACE_URI,
085: XLINK_ARCROLE);
086: String title = attr.getValue(XLINK_NAMESPACE_URI,
087: XLINK_TITLE);
088: String show = attr.getValue(XLINK_NAMESPACE_URI,
089: XLINK_SHOW);
090: String actuate = attr.getValue(XLINK_NAMESPACE_URI,
091: XLINK_ACTUATE);
092: simpleLink(href, role, arcrole, title, show, actuate,
093: uri, name, raw, attr);
094: } else if (type.equals(XLINK_TYPE_EXTENDED)) {
095: if (this .extendedLinkElementName != null) {
096: throw new SAXException(
097: "An XLink extended link cannot include another 'extended' element");
098: } else if (this .linkLocatorElementName != null) {
099: throw new SAXException(
100: "An XLink extended link cannot be included into a 'locator' element");
101: } else if (this .linkArcElementName != null) {
102: throw new SAXException(
103: "An XLink extended link cannot be included into an 'arc' element");
104: }
105: String role = attr.getValue(XLINK_NAMESPACE_URI,
106: XLINK_ROLE);
107: String title = attr.getValue(XLINK_NAMESPACE_URI,
108: XLINK_TITLE);
109: this .extendedLinkElementName = name;
110: this .extendedLinkElementURI = uri;
111: startExtendedLink(role, title, uri, name, raw, attr);
112: } else if (type.equals(XLINK_TYPE_LOCATOR)) {
113: if (this .extendedLinkElementName == null) {
114: throw new SAXException(
115: "An XLink locator must be included into an 'extended' element");
116: } else if (this .linkLocatorElementName != null) {
117: throw new SAXException(
118: "An XLink locator cannot be included into another 'locator' element");
119: } else if (this .linkArcElementName != null) {
120: throw new SAXException(
121: "An XLink locator cannot be included into an 'arc' element");
122: }
123: String href = attr.getValue(XLINK_NAMESPACE_URI,
124: XLINK_HREF);
125: String role = attr.getValue(XLINK_NAMESPACE_URI,
126: XLINK_ROLE);
127: String title = attr.getValue(XLINK_NAMESPACE_URI,
128: XLINK_TITLE);
129: String label = attr.getValue(XLINK_NAMESPACE_URI,
130: XLINK_LABEL);
131: this .linkLocatorElementName = name;
132: this .linkLocatorElementURI = uri;
133: startLocator(href, role, title, label, uri, name, raw,
134: attr);
135: } else if (type.equals(XLINK_TYPE_ARC)) {
136: if (this .extendedLinkElementName == null) {
137: throw new SAXException(
138: "An XLink arc must be included into an 'extended' element");
139: } else if (this .linkLocatorElementName != null) {
140: throw new SAXException(
141: "An XLink arc cannot be included into a 'locator' element");
142: } else if (this .linkArcElementName != null) {
143: throw new SAXException(
144: "An XLink arc cannot be included into another 'arc' element");
145: }
146: String arcrole = attr.getValue(XLINK_NAMESPACE_URI,
147: XLINK_ARCROLE);
148: String title = attr.getValue(XLINK_NAMESPACE_URI,
149: XLINK_TITLE);
150: String show = attr.getValue(XLINK_NAMESPACE_URI,
151: XLINK_SHOW);
152: String actuate = attr.getValue(XLINK_NAMESPACE_URI,
153: XLINK_ACTUATE);
154: String from = attr.getValue(XLINK_NAMESPACE_URI,
155: XLINK_FROM);
156: String to = attr
157: .getValue(XLINK_NAMESPACE_URI, XLINK_TO);
158: this .linkArcElementName = name;
159: this .linkArcElementURI = uri;
160: startArc(arcrole, title, show, actuate, from, to, uri,
161: name, raw, attr);
162: } else if (type.equals(XLINK_TYPE_RESOURCE)) {
163: if (this .extendedLinkElementName == null) {
164: throw new SAXException(
165: "An XLink resource must be included into an 'extended' element");
166: }
167: String role = attr.getValue(XLINK_NAMESPACE_URI,
168: XLINK_ROLE);
169: String title = attr.getValue(XLINK_NAMESPACE_URI,
170: XLINK_TITLE);
171: String label = attr.getValue(XLINK_NAMESPACE_URI,
172: XLINK_LABEL);
173: linkResource(role, title, label, uri, name, raw, attr);
174: } else if (type.equals(XLINK_TYPE_TITLE)) {
175: if ((this .extendedLinkElementName == null)
176: && (this .linkLocatorElementName == null)
177: && (this .linkArcElementName == null)) {
178: throw new SAXException(
179: "An XLink title must be included into an 'extended', 'locator' or 'arc' element");
180: }
181: linkTitle(uri, name, raw, attr);
182: } else {
183: super .startElement(uri, name, raw, attr);
184: }
185: } else {
186: super .startElement(uri, name, raw, attr);
187: }
188: }
189:
190: public void endElement(String uri, String name, String raw)
191: throws SAXException {
192: if ((name.equals(this .extendedLinkElementName))
193: && (uri.equals(this .extendedLinkElementURI))) {
194: this .extendedLinkElementName = null;
195: this .extendedLinkElementURI = null;
196: this .endExtendedLink(uri, name, raw);
197: } else if ((name.equals(this .linkLocatorElementName))
198: && (uri.equals(this .linkLocatorElementURI))) {
199: this .linkLocatorElementName = null;
200: this .linkLocatorElementURI = null;
201: this .endLocator(uri, name, raw);
202: } else if ((name.equals(this .linkArcElementName))
203: && (uri.equals(this .linkArcElementURI))) {
204: this .linkArcElementName = null;
205: this .linkArcElementURI = null;
206: this .endArc(uri, name, raw);
207: } else {
208: super .endElement(uri, name, raw);
209: }
210: }
211:
212: // Default XLinkHandler implementation (defaults to copy over)
213:
214: public void simpleLink(String href, String role, String arcrole,
215: String title, String show, String actuate, String uri,
216: String name, String raw, Attributes attr)
217: throws SAXException {
218: AttributesImpl newattr = new AttributesImpl(attr);
219: int hrefIndex = attr.getIndex(XLINK_NAMESPACE_URI, XLINK_HREF);
220: int roleIndex = attr.getIndex(XLINK_NAMESPACE_URI, XLINK_ROLE);
221: int arcroleIndex = attr.getIndex(XLINK_NAMESPACE_URI,
222: XLINK_ARCROLE);
223: int titleIndex = attr
224: .getIndex(XLINK_NAMESPACE_URI, XLINK_TITLE);
225: int showIndex = attr.getIndex(XLINK_NAMESPACE_URI, XLINK_SHOW);
226: int actuateIndex = attr.getIndex(XLINK_NAMESPACE_URI,
227: XLINK_ACTUATE);
228: if (hrefIndex > -1)
229: newattr.setValue(hrefIndex, href);
230: if (roleIndex > -1)
231: newattr.setValue(roleIndex, role);
232: if (arcroleIndex > -1)
233: newattr.setValue(arcroleIndex, arcrole);
234: if (titleIndex > -1)
235: newattr.setValue(titleIndex, title);
236: if (showIndex > -1)
237: newattr.setValue(showIndex, show);
238: if (actuateIndex > -1)
239: newattr.setValue(actuateIndex, actuate);
240: super .startElement(uri, name, raw, newattr);
241: }
242:
243: public void startExtendedLink(String role, String title,
244: String uri, String name, String raw, Attributes attr)
245: throws SAXException {
246: AttributesImpl newattr = new AttributesImpl(attr);
247: int roleIndex = attr.getIndex(XLINK_NAMESPACE_URI, XLINK_ROLE);
248: int titleIndex = attr
249: .getIndex(XLINK_NAMESPACE_URI, XLINK_TITLE);
250: if (roleIndex > -1)
251: newattr.setValue(roleIndex, role);
252: if (titleIndex > -1)
253: newattr.setValue(titleIndex, title);
254: super .startElement(uri, name, raw, newattr);
255: }
256:
257: public void startLocator(String href, String role, String title,
258: String label, String uri, String name, String raw,
259: Attributes attr) throws SAXException {
260: AttributesImpl newattr = new AttributesImpl(attr);
261: int hrefIndex = attr.getIndex(XLINK_NAMESPACE_URI, XLINK_HREF);
262: int roleIndex = attr.getIndex(XLINK_NAMESPACE_URI, XLINK_ROLE);
263: int titleIndex = attr
264: .getIndex(XLINK_NAMESPACE_URI, XLINK_TITLE);
265: int labelIndex = attr
266: .getIndex(XLINK_NAMESPACE_URI, XLINK_LABEL);
267: if (hrefIndex > -1)
268: newattr.setValue(hrefIndex, href);
269: if (roleIndex > -1)
270: newattr.setValue(roleIndex, role);
271: if (titleIndex > -1)
272: newattr.setValue(titleIndex, title);
273: if (labelIndex > -1)
274: newattr.setValue(labelIndex, label);
275: super .startElement(uri, name, raw, newattr);
276: }
277:
278: public void startArc(String arcrole, String title, String show,
279: String actuate, String from, String to, String uri,
280: String name, String raw, Attributes attr)
281: throws SAXException {
282: AttributesImpl newattr = new AttributesImpl(attr);
283: int arcroleIndex = attr.getIndex(XLINK_NAMESPACE_URI,
284: XLINK_ARCROLE);
285: int titleIndex = attr
286: .getIndex(XLINK_NAMESPACE_URI, XLINK_TITLE);
287: int showIndex = attr.getIndex(XLINK_NAMESPACE_URI, XLINK_SHOW);
288: int actuateIndex = attr.getIndex(XLINK_NAMESPACE_URI,
289: XLINK_ACTUATE);
290: int fromIndex = attr.getIndex(XLINK_NAMESPACE_URI, XLINK_FROM);
291: int toIndex = attr.getIndex(XLINK_NAMESPACE_URI, XLINK_TO);
292: if (arcroleIndex > -1)
293: newattr.setValue(arcroleIndex, arcrole);
294: if (titleIndex > -1)
295: newattr.setValue(titleIndex, title);
296: if (showIndex > -1)
297: newattr.setValue(showIndex, show);
298: if (actuateIndex > -1)
299: newattr.setValue(actuateIndex, actuate);
300: if (fromIndex > -1)
301: newattr.setValue(actuateIndex, from);
302: if (toIndex > -1)
303: newattr.setValue(actuateIndex, to);
304: super .startElement(uri, name, raw, newattr);
305: }
306:
307: public void linkResource(String role, String title, String label,
308: String uri, String name, String raw, Attributes attr)
309: throws SAXException {
310: AttributesImpl newattr = new AttributesImpl(attr);
311: int roleIndex = attr.getIndex(XLINK_NAMESPACE_URI, XLINK_ROLE);
312: int titleIndex = attr
313: .getIndex(XLINK_NAMESPACE_URI, XLINK_TITLE);
314: int labelIndex = attr
315: .getIndex(XLINK_NAMESPACE_URI, XLINK_LABEL);
316: if (roleIndex > -1)
317: newattr.setValue(roleIndex, role);
318: if (titleIndex > -1)
319: newattr.setValue(titleIndex, title);
320: if (labelIndex > -1)
321: newattr.setValue(labelIndex, label);
322: super .startElement(uri, name, raw, newattr);
323: }
324:
325: public void linkTitle(String uri, String name, String raw,
326: Attributes attr) throws SAXException {
327: super .startElement(uri, name, raw, attr);
328: }
329:
330: public void endExtendedLink(String uri, String name, String raw)
331: throws SAXException {
332: super .endElement(uri, name, raw);
333: }
334:
335: public void endLocator(String uri, String name, String raw)
336: throws SAXException {
337: super .endElement(uri, name, raw);
338: }
339:
340: public void endArc(String uri, String name, String raw)
341: throws SAXException {
342: super.endElement(uri, name, raw);
343: }
344: }
|