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.components.xpointer;
018:
019: import org.w3c.dom.Document;
020: import org.apache.excalibur.source.Source;
021: import org.apache.excalibur.xml.xpath.PrefixResolver;
022: import org.apache.avalon.framework.logger.Logger;
023: import org.apache.avalon.framework.service.ServiceManager;
024: import org.apache.cocoon.components.source.SourceUtil;
025: import org.apache.cocoon.xml.XMLConsumer;
026: import org.apache.cocoon.ResourceNotFoundException;
027: import org.xml.sax.SAXException;
028:
029: import java.util.HashMap;
030:
031: /**
032: * A context object used during the evaluating of XPointers.
033: */
034: public class XPointerContext implements PrefixResolver {
035: private Source source;
036: private Document document;
037: private XMLConsumer xmlConsumer;
038: private Logger logger;
039: private String xpointer;
040: private HashMap prefixes = new HashMap();
041: private ServiceManager manager;
042:
043: /**
044: * Constructs an XPointerContext object.
045: *
046: * @param xpointer the original fragment identifier string, used for debugging purposes
047: * @param source the source into which the xpointer points
048: * @param xmlConsumer the consumer to which the result of the xpointer evaluation should be send
049: */
050: public XPointerContext(String xpointer, Source source,
051: XMLConsumer xmlConsumer, Logger logger,
052: ServiceManager manager) {
053: this .source = source;
054: this .xmlConsumer = xmlConsumer;
055: this .logger = logger;
056: this .manager = manager;
057: this .xpointer = xpointer;
058:
059: prefixes.put("xml", "http://www.w3.org/XML/1998/namespace");
060: }
061:
062: public Document getDocument() throws SAXException,
063: ResourceNotFoundException {
064: if (document == null) {
065: try {
066: document = SourceUtil.toDOM(source);
067: } catch (ResourceNotFoundException e) {
068: throw e;
069: } catch (Exception e) {
070: throw new SAXException(
071: "Error during XPointer evaluation while trying to load "
072: + source.getURI(), e);
073: }
074: }
075: return document;
076: }
077:
078: public Source getSource() {
079: return source;
080: }
081:
082: public XMLConsumer getXmlConsumer() {
083: return xmlConsumer;
084: }
085:
086: public Logger getLogger() {
087: return logger;
088: }
089:
090: public String getXPointer() {
091: return xpointer;
092: }
093:
094: public ServiceManager getServiceManager() {
095: return manager;
096: }
097:
098: public void addPrefix(String prefix, String namespace)
099: throws SAXException {
100: // according to the xmlns() scheme spec, these should not result to any change in namespace context
101: if (prefix.equalsIgnoreCase("xml"))
102: return;
103: else if (prefix.equals("xmlns"))
104: return;
105: else if (namespace
106: .equals("http://www.w3.org/XML/1998/namespace"))
107: return;
108: else if (namespace.equals("http://www.w3.org/2000/xmlns/"))
109: return;
110:
111: prefixes.put(prefix, namespace);
112: }
113:
114: public String prefixToNamespace(String prefix) {
115: return (String) prefixes.get(prefix);
116: }
117: }
|