001 /* 002 * Copyright (C) 2006-2007 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.codehaus.gmaven.runtime.v1_6; 018 019 import org.codehaus.gmaven.feature.Component; 020 import org.codehaus.gmaven.feature.support.FeatureSupport; 021 import org.codehaus.gmaven.runtime.StubCompiler; 022 import org.codehaus.gmaven.runtime.support.CompilerSupport; 023 import org.codehaus.gmaven.runtime.support.stubgen.model.ModelFactory; 024 import org.codehaus.gmaven.runtime.support.stubgen.model.SourceDef; 025 import org.codehaus.gmaven.runtime.support.stubgen.parser.SourceType; 026 import org.codehaus.gmaven.runtime.support.stubgen.render.Renderer; 027 import org.codehaus.gmaven.runtime.support.stubgen.render.RendererFactory; 028 import org.codehaus.gmaven.runtime.v1_6.stubgen.ModelFactoryImpl; 029 import org.codehaus.gmaven.runtime.v1_6.stubgen.RendererFactoryImpl; 030 031 import java.io.BufferedWriter; 032 import java.io.File; 033 import java.io.FileWriter; 034 import java.io.IOException; 035 import java.io.PrintWriter; 036 import java.io.Writer; 037 import java.net.URL; 038 import java.util.Iterator; 039 import java.util.Set; 040 041 /** 042 * Provides the stub compilation feature. 043 * 044 * @version $Id: StubCompilerFeature.java 49 2009-10-16 14:03:56Z user57 $ 045 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a> 046 */ 047 public class StubCompilerFeature 048 extends FeatureSupport 049 { 050 public StubCompilerFeature() { 051 super(StubCompiler.KEY); 052 } 053 054 @Override 055 protected Component doCreate() throws Exception { 056 return new StubCompilerImpl(); 057 } 058 059 // 060 // StubCompilerImpl 061 // 062 063 private class StubCompilerImpl 064 extends CompilerSupport 065 implements StubCompiler 066 { 067 private ModelFactory modelFactory = new ModelFactoryImpl(); 068 069 private RendererFactory rendererFactory = new RendererFactoryImpl(); 070 071 private StubCompilerImpl() throws Exception { 072 super(StubCompilerFeature.this); 073 } 074 075 public int compile() throws Exception { 076 if (sources.isEmpty()) { 077 log.debug("No sources added to compile; skipping"); 078 079 return 0; 080 } 081 082 log.debug("Compiling {} stubs for source(s)", String.valueOf(sources.size())); 083 084 int count = 0; 085 086 for (Iterator iter = sources.iterator(); iter.hasNext();) { 087 URL url = (URL) iter.next(); 088 log.debug(" {}", url); 089 090 count += render(url); 091 } 092 093 log.debug("Compiled {} stubs", String.valueOf(count)); 094 095 return count; 096 } 097 098 private int render(final URL url) throws Exception { 099 assert url != null; 100 101 SourceDef model = modelFactory.create(url); 102 103 Set renderers = rendererFactory.create(model); 104 105 Iterator iter = renderers.iterator(); 106 107 int count = 0; 108 109 while (iter.hasNext()) { 110 Renderer renderer = (Renderer)iter.next(); 111 112 Writer writer = createWriter(renderer, getTargetDirectory()); 113 114 try { 115 renderer.render(writer); 116 count ++; 117 } 118 finally { 119 writer.close(); 120 } 121 } 122 123 return count; 124 } 125 126 private PrintWriter createWriter(final Renderer renderer, final File outputDir) throws IOException { 127 assert renderer != null; 128 assert outputDir != null; 129 130 StringBuffer buff = new StringBuffer(); 131 132 String pkg = renderer.getPackage(); 133 134 if (pkg != null) { 135 buff.append(pkg.replace('.', '/')); 136 buff.append("/"); 137 } 138 139 buff.append(renderer.getName()); 140 buff.append(SourceType.JAVA_EXT); 141 142 File outputFile = new File(outputDir, buff.toString()); 143 144 outputFile.getParentFile().mkdirs(); 145 146 return new PrintWriter(new BufferedWriter(new FileWriter(outputFile))); 147 } 148 } 149 }