KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > impl > XPathSourceInspector


1 /*
2  * Copyright 1999-2005 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.source.impl;
17
18 import java.io.IOException JavaDoc;
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 JavaDoc;
37 import org.w3c.dom.NodeList JavaDoc;
38 import org.xml.sax.InputSource JavaDoc;
39 import org.xml.sax.SAXException JavaDoc;
40
41 /**
42  * This source inspector inspects XML files with a xpath expression.
43  *
44  * @version $Id: XPathSourceInspector.java 170998 2005-05-19 21:05:36Z vgritsenko $
45  */

46 public class XPathSourceInspector extends AbstractLogEnabled
47                                   implements SourceInspector, Serviceable,
48                                              Parameterizable, ThreadSafe {
49
50     /**
51      * The default namespace uri of the property exposed by this SourceInspector.
52      * <p>
53      * The value is <code>http://apache.org/cocoon/inspector/xpath/1.0</code>.
54      * </p>
55      */

56     public static final String JavaDoc DEFAULT_PROPERTY_NS = "http://apache.org/cocoon/inspector/xpath/1.0";
57
58     /**
59      * The default property name exposed by this SourceInspector.
60      * <p>
61      * The value is <code>result</code> .
62      * </p>
63      */

64     public static final String JavaDoc DEFAULT_PROPERTY_NAME = "result";
65
66     private String JavaDoc m_namespace;
67     private String JavaDoc m_propertyname;
68     private String JavaDoc m_extension;
69     private String JavaDoc 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 JavaDoc namespace, String JavaDoc 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 JavaDoc doc = null;
97             try {
98                 parser = (DOMParser) manager.lookup(DOMParser.ROLE);
99                 InputSource JavaDoc is = new InputSource JavaDoc(source.getInputStream());
100                 is.setSystemId(source.getURI());
101                 doc = parser.parseDocument(is);
102             } catch (SAXException JavaDoc se) {
103                 getLogger().error(source.getURI() + " is not a valid XML file");
104             } catch (IOException JavaDoc 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 JavaDoc 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 JavaDoc namespace, String JavaDoc name) {
142         return this.m_namespace.equals(namespace) && this.m_propertyname.equals(name);
143     }
144
145     /**
146      * Returns NOPValidity
147      */

148     public SourceValidity getValidity(Source source) {
149         return NOPValidity.SHARED_INSTANCE;
150     }
151
152 }
153
Popular Tags