1 16 package org.apache.cocoon.components.xpointer; 17 18 import org.w3c.dom.Document ; 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 ; 27 28 import java.util.HashMap ; 29 30 33 public class XPointerContext implements PrefixResolver { 34 private Source source; 35 private Document document; 36 private XMLConsumer xmlConsumer; 37 private Logger logger; 38 private String xpointer; 39 private HashMap prefixes = new HashMap (); 40 private ServiceManager manager; 41 42 49 public XPointerContext(String 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 getDocument() throws SAXException , ResourceNotFoundException { 60 if (document == null) { 61 try { 62 document = SourceUtil.toDOM(source); 63 } catch (ResourceNotFoundException e) { 64 throw e; 65 } catch (Exception e) { 66 throw new SAXException ("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 getXPointer() { 85 return xpointer; 86 } 87 88 public ServiceManager getServiceManager() { 89 return manager; 90 } 91 92 public void addPrefix(String prefix, String namespace) throws SAXException { 93 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 prefixToNamespace(String prefix) { 107 return (String )prefixes.get(prefix); 108 } 109 } 110 | Popular Tags |