KickJava   Java API By Example, From Geeks To Geeks.

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


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.source.impl;
17
18
19 import java.io.InputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.net.MalformedURLException JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.logger.Logger;
27 import org.apache.avalon.framework.service.ServiceException;
28 import org.apache.avalon.framework.service.ServiceManager;
29 import org.apache.avalon.framework.service.ServiceSelector;
30
31 import org.apache.excalibur.source.SourceException;
32 import org.apache.excalibur.source.impl.AbstractSource;
33
34 import org.apache.cocoon.components.modules.input.InputModule;
35
36 import org.apache.commons.jxpath.JXPathContext;
37
38
39 /**
40  * A <code>Source</code> that takes its content from a
41  * module.
42  * <p>The URI syntax is
43  * "module:<input-module>:<attribute-name>[#XPath]",
44  * where :
45  * <ul>
46  * <li>an input-module name is used for finding an input-module for reading data from</li>,
47  * <li>"attribute-name" is the name of the attribute found in the module</li>,
48  * <li>"XPath" is an XPath that is aplied on the object in the
49  * attribute, by using JXPath.</li>
50  * </ul>
51  * </p>
52  *
53  * @author <a HREF="mailto:danielf@nada.kth.se">Daniel Fagerstom</a>
54  */

55
56 public class ModuleSource
57     extends AbstractSource {
58
59     private final static String JavaDoc SCHEME = "module";
60     private String JavaDoc attributeType;
61     private String JavaDoc attributeName;
62     private String JavaDoc xPath;
63     private ServiceManager manager;
64     private Map JavaDoc objectModel;
65     private Logger logger;
66     
67     /**
68      * Create a module source from a 'module:' uri and a the object model.
69      * <p>The uri is of the form "module:attribute-type:attribute-name#xpath</p>
70      */

71     public ModuleSource( Map JavaDoc objectModel, String JavaDoc uri,
72                          ServiceManager manager, Logger logger )
73         throws MalformedURLException JavaDoc {
74
75         this.objectModel = objectModel;
76         this.manager = manager;
77         this.logger = logger;
78
79         setSystemId( uri );
80
81         // Scheme
82
int start = 0;
83         int end = uri.indexOf( ':' );
84         if ( end == -1 )
85             throw new MalformedURLException JavaDoc("Malformed uri for module source (cannot find scheme) : " + uri);
86
87         String JavaDoc scheme = uri.substring( start, end );
88         if ( !SCHEME.equals( scheme ) )
89             throw new MalformedURLException JavaDoc("Malformed uri for a module source : " + uri);
90
91         setScheme( scheme );
92
93         // Attribute type
94
start = end + 1;
95         end = uri.indexOf( ':', start );
96         if ( end == -1 ) {
97             throw new MalformedURLException JavaDoc("Malformed uri for module source (cannot find attribute type) : " + uri);
98         }
99         this.attributeType = uri.substring( start, end );
100
101         // Attribute name
102
start = end + 1;
103         end = uri.indexOf( '#', start );
104
105         if ( end == -1 )
106             end = uri.length();
107
108         if ( end == start )
109             throw new MalformedURLException JavaDoc("Malformed uri for module source (cannot find attribute name) : " + uri);
110
111         this.attributeName = uri.substring( start, end );
112
113         // xpath
114
start = end + 1;
115         this.xPath = start < uri.length() ? uri.substring( start ) : "";
116     }
117     
118     /**
119      * Return an <code>InputStream</code> object to read from the source.
120      *
121      * @throws IOException if I/O error occured.
122      */

123     public InputStream JavaDoc getInputStream() throws IOException JavaDoc, SourceException {
124         if ( this.logger.isDebugEnabled() ) {
125             this.logger.debug( "Getting InputStream for " + getURI() );
126         }
127
128         Object JavaDoc obj = getInputAttribute( this.attributeType, this.attributeName );
129         if ( obj == null )
130             throw new SourceException( " The attribute: " + this.attributeName +
131                                        " is empty" );
132
133         if ( !(this.xPath.length() == 0 || this.xPath.equals( "/" )) ) {
134             JXPathContext context = JXPathContext.newContext( obj );
135             obj = context.getValue( this.xPath );
136
137             if ( obj == null )
138                 throw new SourceException( "the xpath: " + this.xPath +
139                                            " applied on the attribute: " +
140                                            this.attributeName +
141                                            " returns null");
142         }
143
144         if ( obj instanceof InputStream JavaDoc ) {
145             return (InputStream JavaDoc)obj;
146         } else if ( obj instanceof String JavaDoc ) {
147             return new ByteArrayInputStream JavaDoc( ((String JavaDoc)obj).getBytes() );
148         } else if (obj instanceof byte[]) {
149             return new ByteArrayInputStream JavaDoc((byte[]) obj);
150         } else {
151             throw new SourceException( "The object type: " + obj.getClass() +
152                                        " could not be serialized as a InputStream " + obj );
153         }
154     }
155
156     /**
157      * Does this source actually exist ?
158      *
159      * @return true if the resource exists.
160      *
161      */

162     public boolean exists() {
163         boolean exists = false;
164         try {
165             exists = getInputAttribute( this.attributeType, this.attributeName ) != null;
166         } catch ( SourceException e ) {
167             exists = false;
168         }
169         return exists;
170     }
171
172     private Object JavaDoc getInputAttribute( String JavaDoc inputModuleName, String JavaDoc attributeName )
173         throws SourceException {
174         Object JavaDoc obj;
175         ServiceSelector selector = null;
176         InputModule inputModule = null;
177         try {
178             selector = (ServiceSelector) this.manager.lookup( InputModule.ROLE + "Selector" );
179             inputModule = (InputModule) selector.select( inputModuleName );
180             obj = inputModule.getAttribute( attributeName, null, this.objectModel );
181
182         } catch ( ServiceException e ) {
183             throw new SourceException( "Could not find an InputModule of the type " +
184                                        inputModuleName , e );
185         } catch ( ConfigurationException e ) {
186             throw new SourceException( "Could not find an attribute: " + attributeName +
187                                        " from the InputModule " + inputModuleName, e );
188         } finally {
189             if ( inputModule != null ) selector.release( inputModule );
190             this.manager.release( selector );
191         }
192
193         return obj;
194     }
195 }
196
Popular Tags