1 16 package org.apache.cocoon.components.source.impl; 17 18 19 import java.io.InputStream ; 20 import java.io.IOException ; 21 import java.io.ByteArrayInputStream ; 22 import java.net.MalformedURLException ; 23 import java.util.Map ; 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 55 56 public class ModuleSource 57 extends AbstractSource { 58 59 private final static String SCHEME = "module"; 60 private String attributeType; 61 private String attributeName; 62 private String xPath; 63 private ServiceManager manager; 64 private Map objectModel; 65 private Logger logger; 66 67 71 public ModuleSource( Map objectModel, String uri, 72 ServiceManager manager, Logger logger ) 73 throws MalformedURLException { 74 75 this.objectModel = objectModel; 76 this.manager = manager; 77 this.logger = logger; 78 79 setSystemId( uri ); 80 81 int start = 0; 83 int end = uri.indexOf( ':' ); 84 if ( end == -1 ) 85 throw new MalformedURLException ("Malformed uri for module source (cannot find scheme) : " + uri); 86 87 String scheme = uri.substring( start, end ); 88 if ( !SCHEME.equals( scheme ) ) 89 throw new MalformedURLException ("Malformed uri for a module source : " + uri); 90 91 setScheme( scheme ); 92 93 start = end + 1; 95 end = uri.indexOf( ':', start ); 96 if ( end == -1 ) { 97 throw new MalformedURLException ("Malformed uri for module source (cannot find attribute type) : " + uri); 98 } 99 this.attributeType = uri.substring( start, end ); 100 101 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 ("Malformed uri for module source (cannot find attribute name) : " + uri); 110 111 this.attributeName = uri.substring( start, end ); 112 113 start = end + 1; 115 this.xPath = start < uri.length() ? uri.substring( start ) : ""; 116 } 117 118 123 public InputStream getInputStream() throws IOException , SourceException { 124 if ( this.logger.isDebugEnabled() ) { 125 this.logger.debug( "Getting InputStream for " + getURI() ); 126 } 127 128 Object 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 ) { 145 return (InputStream )obj; 146 } else if ( obj instanceof String ) { 147 return new ByteArrayInputStream ( ((String )obj).getBytes() ); 148 } else if (obj instanceof byte[]) { 149 return new ByteArrayInputStream ((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 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 getInputAttribute( String inputModuleName, String attributeName ) 173 throws SourceException { 174 Object 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 |