1 16 package org.apache.cocoon.components.source.impl; 17 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.net.MalformedURLException ; 22 import java.util.ArrayList ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Map ; 26 27 import org.apache.avalon.framework.configuration.Configurable; 28 import org.apache.avalon.framework.configuration.Configuration; 29 import org.apache.avalon.framework.configuration.ConfigurationException; 30 import org.apache.avalon.framework.logger.AbstractLogEnabled; 31 import org.apache.avalon.framework.service.ServiceException; 32 import org.apache.avalon.framework.service.ServiceManager; 33 import org.apache.avalon.framework.service.Serviceable; 34 import org.apache.avalon.framework.thread.ThreadSafe; 35 import org.apache.excalibur.source.Source; 36 import org.apache.excalibur.source.SourceException; 37 import org.apache.excalibur.source.SourceFactory; 38 import org.apache.excalibur.source.SourceResolver; 39 import org.apache.regexp.RE; 40 import org.apache.regexp.RESyntaxException; 41 42 43 79 public final class QDoxSourceFactory 80 extends AbstractLogEnabled 81 implements SourceFactory, Serviceable, Configurable, ThreadSafe { 82 83 protected final static String INCLUDE_INHERITANCE_ELEMENT = "include-inheritance"; 84 protected final static String VALUE_ATTRIBUTE = "value"; 85 protected final static String SOURCE_GROUP_ELEMENT = "source-roots"; 86 protected final static String GROUP_ATTRIBUTE = "group"; 87 protected final static String SOURCE_ROOT_ELEMENT = "source-root"; 88 protected final static String URI_ATTRIBUTE = "uri"; 89 90 protected ServiceManager manager; 91 protected List sourceRootUris; 92 93 96 protected RE rePackageClass; 97 98 101 protected RE rePackageClassInnerclass; 102 103 104 110 protected static final class SourceRoot { 111 private List packages; 112 private String sourceRootUri; 113 114 protected SourceRoot(String uri) { 115 if (!uri.endsWith(File.separator)) { 116 uri += '/'; 117 } 118 sourceRootUri = uri; 119 packages = new ArrayList (); 120 } 121 122 protected void addPackage(String packageName) { 123 packages.add(packageName); 124 } 125 126 protected boolean hasPackage(String packageName) { 127 return packages.contains(packageName); 128 } 129 130 protected String getUri() { 131 return sourceRootUri; 132 } 133 } 134 135 136 139 public Source getSource(String location, Map parameters) throws MalformedURLException , IOException , SourceException { 140 String className = location.substring(location.indexOf(':') + 1); 141 Source javaSource = null; 142 if (className.length() > 0) { 143 try { 144 if(getLogger().isDebugEnabled()) { 145 getLogger().debug("getSource called with className=" + className); 146 } 147 javaSource = getSource(className); 148 } catch (ServiceException se) { 149 throw new SourceException("SourceResolver not found", se); 150 } 151 } else { 152 throw new MalformedURLException (); 153 } 154 155 QDoxSource result = null; 156 if (javaSource != null) { 157 return new QDoxSource(location, javaSource, getLogger(), manager); 158 } 159 160 if(getLogger().isDebugEnabled()) { 161 getLogger().debug("returning source=" + result + " for className=" + className); 162 } 163 164 return result; 165 } 166 167 170 public void service(ServiceManager manager) throws ServiceException { 171 if (getLogger().isDebugEnabled()) { 172 getLogger().debug("Composing the QDoxSourceFactory..."); 173 } 174 this.manager = manager; 175 176 try { 177 rePackageClass = new RE("([$\\w.]+)\\.([$\\w]+)"); 178 rePackageClassInnerclass = new RE("([$\\w.]+)\\.([$\\w]+)\\.([$\\w]+)"); 179 } catch (RESyntaxException e) { 180 getLogger().error("RegExp syntax error!", e); 181 } 182 } 183 184 187 public void configure(Configuration config) throws ConfigurationException { 188 Configuration[] sourceRootGroups = config.getChildren(SOURCE_GROUP_ELEMENT); 189 sourceRootUris = new ArrayList (); 190 191 for (int i=0; i<sourceRootGroups.length; i++) { 192 Configuration[] sourceRootConfigs = sourceRootGroups[i].getChildren(SOURCE_ROOT_ELEMENT); 193 194 for (int j=0; j<sourceRootConfigs.length; j++) { 195 String uri = sourceRootConfigs[j].getAttribute(URI_ATTRIBUTE); 196 sourceRootUris.add(new SourceRoot(uri)); 197 } 198 } 199 200 if (sourceRootUris.size() == 0 && getLogger().isErrorEnabled()) { 201 getLogger().error("No source roots configured!"); 202 } 203 } 204 205 210 public void release(Source source) { 211 } 213 214 220 private Source getSource(String className) throws ServiceException { 221 String classFileName = className; 222 String packageName; 223 224 if (rePackageClass.match(className)) { 225 packageName = rePackageClass.getParen(1); 226 } else { 227 packageName = ""; 228 } 229 230 classFileName = classFileName.replace('.', '/') + ".java"; 231 SourceResolver resolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 232 233 Source source = getSource(classFileName, packageName, resolver); 234 if (source == null && rePackageClassInnerclass.match(className)) { 235 237 packageName = rePackageClassInnerclass.getParen(1); 238 classFileName = className.substring(0, className.lastIndexOf('.')).replace('.', '/') + ".java"; 239 source = getSource(classFileName, packageName, resolver); 240 } 241 manager.release(resolver); 242 243 if (source == null && getLogger().isWarnEnabled()) { 244 getLogger().warn("No source found for class '" + className + "'!"); 245 } 246 247 return source; 248 } 249 250 private Source getSource(String classFileName, String packageName, SourceResolver resolver) { 251 for (Iterator i = sourceRootUris.iterator(); i.hasNext();) { 253 SourceRoot sourceRoot = (SourceRoot) i.next(); 254 255 if (sourceRoot.hasPackage(packageName)) { 256 String uri = sourceRoot.getUri() + classFileName; 257 Source source = getSource(uri, resolver); 258 if (source != null) { 259 return source; 260 } 261 } 262 } 263 264 for (Iterator i = sourceRootUris.iterator(); i.hasNext();) { 266 SourceRoot sourceRoot = (SourceRoot) i.next(); 267 String uri = sourceRoot.getUri() + classFileName; 268 269 Source source = getSource(uri, resolver); 270 if (source != null) { 271 sourceRoot.addPackage(packageName); 272 return source; 273 } 274 } 275 return null; 276 } 277 278 285 private Source getSource(String uri, SourceResolver resolver) { 286 if (getLogger().isDebugEnabled()) { 287 getLogger().debug("Testing uri <" + uri + ">..."); 288 } 289 try { 290 Source source = resolver.resolveURI(uri); 291 292 if (source != null && source.getInputStream() != null) { 293 return source; 294 } else { 295 if (getLogger().isDebugEnabled()) { 296 getLogger().debug("uri <" + uri + "> is invalid."); 297 } 298 } 299 } catch (Exception e) { 300 if (getLogger().isDebugEnabled()) { 301 getLogger().debug("uri <" + uri + "> is invalid: " + e.getClass().getName() + " says " + e.getMessage()); 302 } 303 } 304 return null; 305 } 306 } 307 | Popular Tags |