KickJava   Java API By Example, From Geeks To Geeks.

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


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.avalon.framework.service.ServiceManager;
19 import org.apache.cocoon.xml.XMLConsumer;
20 import org.apache.cocoon.xml.dom.DOMStreamer;
21 import org.apache.cocoon.ResourceNotFoundException;
22 import org.apache.excalibur.xml.xpath.XPathProcessor;
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.NodeList JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26 import org.xml.sax.helpers.LocatorImpl JavaDoc;
27
28 /**
29  * Partly implementation of the xpointer() scheme. Only the XPath subset of xpointer is supported.
30  */

31 public class XPointerPart implements PointerPart {
32     private String JavaDoc expression;
33
34     public XPointerPart(String JavaDoc expression) {
35         this.expression = expression;
36     }
37
38     public boolean process(XPointerContext xpointerContext) throws SAXException JavaDoc, ResourceNotFoundException {
39         Document JavaDoc document = xpointerContext.getDocument();
40         ServiceManager manager = xpointerContext.getServiceManager();
41         XPathProcessor xpathProcessor = null;
42         try {
43             try {
44                 xpathProcessor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE);
45             } catch (Exception JavaDoc e) {
46                 throw new SAXException JavaDoc("XPointerPart: error looking up XPathProcessor.", e);
47             }
48             NodeList JavaDoc nodeList = xpathProcessor.selectNodeList(document, expression, xpointerContext);
49             if (nodeList.getLength() > 0) {
50                 XMLConsumer consumer = xpointerContext.getXmlConsumer();
51                 LocatorImpl JavaDoc locator = new LocatorImpl JavaDoc();
52                 locator.setSystemId(xpointerContext.getSource().getURI());
53                 consumer.setDocumentLocator(locator);
54                 for (int i = 0; i < nodeList.getLength(); i++) {
55                     DOMStreamer streamer = new DOMStreamer();
56                     streamer.setConsumer(consumer);
57                     streamer.stream(nodeList.item(i));
58                 }
59                 return true;
60             } else {
61                 if (xpointerContext.getLogger().isDebugEnabled())
62                     xpointerContext.getLogger().debug("XPointer: expression \"" + expression + "\" gave no results.");
63                 return false;
64             }
65         } finally {
66             if (xpathProcessor != null)
67                 manager.release(xpathProcessor);
68         }
69     }
70 }
71
Popular Tags