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_5;
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_5.stubgen.ModelFactoryImpl;
029    import org.codehaus.gmaven.runtime.v1_5.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 19 2009-07-16 09:40:33Z 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        protected Component doCreate() throws Exception {
055            return new StubCompilerImpl();
056        }
057    
058        //
059        // StubCompilerImpl
060        //
061    
062        private class StubCompilerImpl
063            extends CompilerSupport
064            implements StubCompiler
065        {
066            private ModelFactory modelFactory = new ModelFactoryImpl();
067    
068            private RendererFactory rendererFactory = new RendererFactoryImpl();
069    
070            private StubCompilerImpl() throws Exception {
071                super(StubCompilerFeature.this);
072            }
073    
074            public int compile() throws Exception {
075                if (sources.isEmpty()) {
076                    log.debug("No sources added to compile; skipping");
077    
078                    return 0;
079                }
080    
081                log.debug("Compiling {} stubs for source(s)", String.valueOf(sources.size()));
082    
083                int count = 0;
084    
085                for (Iterator iter = sources.iterator(); iter.hasNext();) {
086                    URL url = (URL) iter.next();
087                    log.debug("    {}", url);
088    
089                    count += render(url);
090                }
091    
092                log.debug("Compiled {} stubs", String.valueOf(count));
093    
094                return count;
095            }
096    
097            private int render(final URL url) throws Exception {
098                assert url != null;
099                
100                SourceDef model = modelFactory.create(url);
101    
102                Set renderers = rendererFactory.create(model);
103    
104                Iterator iter = renderers.iterator();
105    
106                int count = 0;
107    
108                while (iter.hasNext()) {
109                    Renderer renderer = (Renderer)iter.next();
110    
111                    Writer writer = createWriter(renderer, getTargetDirectory());
112    
113                    try {
114                        renderer.render(writer);
115                        count ++;
116                    }
117                    finally {
118                        writer.close();
119                    }
120                }
121    
122                return count;
123            }
124    
125            private PrintWriter createWriter(final Renderer renderer, final File outputDir) throws IOException {
126                assert renderer != null;
127                assert outputDir != null;
128    
129                StringBuffer buff = new StringBuffer();
130    
131                String pkg = renderer.getPackage();
132    
133                if (pkg != null) {
134                    buff.append(pkg.replace('.', '/'));
135                    buff.append("/");
136                }
137    
138                buff.append(renderer.getName());
139                buff.append(SourceType.JAVA_EXT);
140    
141                File outputFile = new File(outputDir, buff.toString());
142    
143                outputFile.getParentFile().mkdirs();
144    
145                return new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
146            }
147        }
148    }