1 16 package org.apache.cocoon.components.source.impl; 17 18 import java.io.IOException ; 19 20 import org.apache.avalon.framework.logger.AbstractLogEnabled; 21 import org.apache.avalon.framework.parameters.ParameterException; 22 import org.apache.avalon.framework.parameters.Parameterizable; 23 import org.apache.avalon.framework.parameters.Parameters; 24 import org.apache.avalon.framework.service.ServiceException; 25 import org.apache.avalon.framework.service.ServiceManager; 26 import org.apache.avalon.framework.service.Serviceable; 27 import org.apache.avalon.framework.thread.ThreadSafe; 28 import org.apache.cocoon.components.source.SourceInspector; 29 import org.apache.cocoon.components.source.helpers.SourceProperty; 30 import org.apache.excalibur.source.Source; 31 import org.apache.excalibur.source.SourceException; 32 import org.apache.excalibur.source.SourceValidity; 33 import org.apache.excalibur.source.impl.validity.NOPValidity; 34 import org.apache.excalibur.xml.dom.DOMParser; 35 import org.apache.excalibur.xml.xpath.XPathProcessor; 36 import org.w3c.dom.Document ; 37 import org.w3c.dom.NodeList ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.SAXException ; 40 41 46 public class XPathSourceInspector extends AbstractLogEnabled 47 implements SourceInspector, Serviceable, 48 Parameterizable, ThreadSafe { 49 50 56 public static final String DEFAULT_PROPERTY_NS = "http://apache.org/cocoon/inspector/xpath/1.0"; 57 58 64 public static final String DEFAULT_PROPERTY_NAME = "result"; 65 66 private String m_namespace; 67 private String m_propertyname; 68 private String m_extension; 69 private String m_xpath; 70 71 private ServiceManager manager; 72 73 74 public XPathSourceInspector() { 75 } 76 77 public void service(ServiceManager manager) { 78 this.manager = manager; 79 } 80 81 public void parameterize(Parameters params) throws ParameterException { 82 this.m_namespace = params.getParameter("namespace", DEFAULT_PROPERTY_NS); 83 this.m_propertyname = params.getParameter("name", DEFAULT_PROPERTY_NAME); 84 this.m_extension = params.getParameter("extension", ".xml"); 85 this.m_xpath = params.getParameter("xpath", "/*"); 86 } 87 88 public SourceProperty getSourceProperty(Source source, String namespace, String name) 89 throws SourceException { 90 91 if ((namespace.equals(m_namespace)) && 92 (name.equals(m_propertyname)) && 93 (source.getURI().endsWith(m_extension))) { 94 95 DOMParser parser = null; 96 Document doc = null; 97 try { 98 parser = (DOMParser) manager.lookup(DOMParser.ROLE); 99 InputSource is = new InputSource (source.getInputStream()); 100 is.setSystemId(source.getURI()); 101 doc = parser.parseDocument(is); 102 } catch (SAXException se) { 103 getLogger().error(source.getURI() + " is not a valid XML file"); 104 } catch (IOException ioe) { 105 getLogger().error("Could not read file", ioe); 106 } catch (ServiceException ce) { 107 getLogger().error("Missing service dependency: DOMParser", ce); 108 } finally { 109 if (parser != null) { 110 this.manager.release(parser); 111 } 112 } 113 114 if (doc != null) { 115 XPathProcessor processor = null; 116 try { 117 processor = (XPathProcessor)manager.lookup(XPathProcessor.ROLE); 118 NodeList nodelist = processor.selectNodeList(doc.getDocumentElement(), m_xpath); 119 SourceProperty property = new SourceProperty(m_namespace, m_propertyname); 120 property.setValue(nodelist); 121 return property; 122 } catch (ServiceException se) { 123 this.getLogger().error("Could not retrieve component", se); 124 } finally { 125 if (processor != null) { 126 this.manager.release(processor); 127 } 128 } 129 } 130 } 131 return null; 132 } 133 134 public SourceProperty[] getSourceProperties(Source source) throws SourceException { 135 SourceProperty property = getSourceProperty(source, this.m_namespace, this.m_propertyname); 136 if (property!=null) 137 return new SourceProperty[]{property}; 138 return null; 139 } 140 141 public boolean handlesProperty(String namespace, String name) { 142 return this.m_namespace.equals(namespace) && this.m_propertyname.equals(name); 143 } 144 145 148 public SourceValidity getValidity(Source source) { 149 return NOPValidity.SHARED_INSTANCE; 150 } 151 152 } 153 | Popular Tags |