01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.xpointer;
18:
19: import org.apache.cocoon.xml.dom.DOMStreamer;
20: import org.apache.cocoon.ResourceNotFoundException;
21: import org.w3c.dom.Document;
22: import org.w3c.dom.Element;
23: import org.xml.sax.SAXException;
24:
25: /**
26: * Implements support for shorthand XPointers (= id-based lookup). We treat them here as if they
27: * were a pointerpart too.
28: *
29: * <p>Note that although this is implemented here, this feature depends on the presence of a DTD,
30: * and a validating parser. Currently, this means its unuseable within Cocoon.
31: */
32: public class ShorthandPart implements PointerPart {
33: private String shorthand;
34:
35: public ShorthandPart(String shorthand) {
36: this .shorthand = shorthand;
37: }
38:
39: public boolean process(XPointerContext xpointerContext)
40: throws SAXException, ResourceNotFoundException {
41: Document document = xpointerContext.getDocument();
42: Element element = document.getElementById(shorthand);
43: if (element != null) {
44: DOMStreamer streamer = new DOMStreamer();
45: streamer.setConsumer(xpointerContext.getXmlConsumer());
46: streamer.stream(element);
47: return true;
48: } else {
49: if (xpointerContext.getLogger().isDebugEnabled())
50: xpointerContext.getLogger().debug(
51: "XPointer: found no element with id "
52: + shorthand + " in document "
53: + xpointerContext.getSource().getURI());
54: }
55: return false;
56: }
57: }
|