KickJava   Java API By Example, From Geeks To Geeks.

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


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.w3c.dom.Document JavaDoc;
19 import org.apache.excalibur.source.Source;
20 import org.apache.excalibur.xml.xpath.PrefixResolver;
21 import org.apache.avalon.framework.logger.Logger;
22 import org.apache.avalon.framework.service.ServiceManager;
23 import org.apache.cocoon.components.source.SourceUtil;
24 import org.apache.cocoon.xml.XMLConsumer;
25 import org.apache.cocoon.ResourceNotFoundException;
26 import org.xml.sax.SAXException JavaDoc;
27
28 import java.util.HashMap JavaDoc;
29
30 /**
31  * A context object used during the evaluating of XPointers.
32  */

33 public class XPointerContext implements PrefixResolver {
34     private Source source;
35     private Document JavaDoc document;
36     private XMLConsumer xmlConsumer;
37     private Logger logger;
38     private String JavaDoc xpointer;
39     private HashMap JavaDoc prefixes = new HashMap JavaDoc();
40     private ServiceManager manager;
41
42     /**
43      * Constructs an XPointerContext object.
44      *
45      * @param xpointer the original fragment identifier string, used for debugging purposes
46      * @param source the source into which the xpointer points
47      * @param xmlConsumer the consumer to which the result of the xpointer evaluation should be send
48      */

49     public XPointerContext(String JavaDoc xpointer, Source source, XMLConsumer xmlConsumer, Logger logger, ServiceManager manager) {
50         this.source = source;
51         this.xmlConsumer = xmlConsumer;
52         this.logger = logger;
53         this.manager = manager;
54         this.xpointer = xpointer;
55
56         prefixes.put("xml", "http://www.w3.org/XML/1998/namespace");
57     }
58
59     public Document JavaDoc getDocument() throws SAXException JavaDoc, ResourceNotFoundException {
60         if (document == null) {
61             try {
62                 document = SourceUtil.toDOM(source);
63             } catch (ResourceNotFoundException e) {
64                 throw e;
65             } catch (Exception JavaDoc e) {
66                 throw new SAXException JavaDoc("Error during XPointer evaluation while trying to load " + source.getURI(), e);
67             }
68         }
69         return document;
70     }
71
72     public Source getSource() {
73         return source;
74     }
75
76     public XMLConsumer getXmlConsumer() {
77         return xmlConsumer;
78     }
79
80     public Logger getLogger() {
81         return logger;
82     }
83
84     public String JavaDoc getXPointer() {
85         return xpointer;
86     }
87
88     public ServiceManager getServiceManager() {
89         return manager;
90     }
91
92     public void addPrefix(String JavaDoc prefix, String JavaDoc namespace) throws SAXException JavaDoc {
93         // according to the xmlns() scheme spec, these should not result to any change in namespace context
94
if (prefix.equalsIgnoreCase("xml"))
95             return;
96         else if (prefix.equals("xmlns"))
97             return;
98         else if (namespace.equals("http://www.w3.org/XML/1998/namespace"))
99             return;
100         else if (namespace.equals("http://www.w3.org/2000/xmlns/"))
101             return;
102
103         prefixes.put(prefix, namespace);
104     }
105
106     public String JavaDoc prefixToNamespace(String JavaDoc prefix) {
107         return (String JavaDoc)prefixes.get(prefix);
108     }
109 }
110
Popular Tags