KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > xpointer > ShorthandPart


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.xpointer;
17
18 import org.apache.cocoon.xml.dom.DOMStreamer;
19 import org.apache.cocoon.ResourceNotFoundException;
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 /**
25  * Implements support for shorthand XPointers (= id-based lookup). We treat them here as if they
26  * were a pointerpart too.
27  *
28  * <p>Note that although this is implemented here, this feature depends on the presence of a DTD,
29  * and a validating parser. Currently, this means its unuseable within Cocoon.
30  */

31 public class ShorthandPart implements PointerPart {
32     private String JavaDoc shorthand;
33
34     public ShorthandPart(String JavaDoc shorthand) {
35         this.shorthand = shorthand;
36     }
37
38     public boolean process(XPointerContext xpointerContext) throws SAXException JavaDoc, ResourceNotFoundException {
39         Document JavaDoc document = xpointerContext.getDocument();
40         Element JavaDoc element = document.getElementById(shorthand);
41         if (element != null) {
42             DOMStreamer streamer = new DOMStreamer();
43             streamer.setConsumer(xpointerContext.getXmlConsumer());
44             streamer.stream(element);
45             return true;
46         } else {
47             if (xpointerContext.getLogger().isDebugEnabled())
48                 xpointerContext.getLogger().debug("XPointer: found no element with id " + shorthand + " in document " + xpointerContext.getSource().getURI());
49         }
50         return false;
51     }
52 }
53
Popular Tags