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.support.stubgen.model;
018    
019    import org.codehaus.gmaven.runtime.support.stubgen.parser.Node;
020    import org.codehaus.gmaven.runtime.support.stubgen.parser.SourceType;
021    import org.codehaus.plexus.util.FileUtils;
022    
023    import java.io.File;
024    import java.net.URI;
025    import java.net.URISyntaxException;
026    import java.net.URL;
027    import java.util.LinkedHashSet;
028    import java.util.LinkedList;
029    import java.util.List;
030    import java.util.Set;
031    
032    /**
033     * Representation of a source file definition.
034     *
035     * @version $Id: SourceDef.java 18 2009-07-16 09:39:40Z user57 $
036     * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
037     */
038    public class SourceDef
039        extends Element
040    {
041        private URL url;
042    
043        private SourceType type;
044    
045        private PackageDef pkg;
046    
047        private final Set imports = new LinkedHashSet();
048    
049        private final Set classes = new LinkedHashSet();
050    
051        private final List statements = new LinkedList();
052    
053        public URL getUrl() {
054            return url;
055        }
056    
057        public void setUrl(final URL url) {
058            this.url = url;
059        }
060    
061        public SourceType getType() {
062            return type;
063        }
064    
065        public void setType(final SourceType type) {
066            this.type = type;
067        }
068    
069        public PackageDef getPackage() {
070            return pkg;
071        }
072    
073        public void setPackage(final PackageDef pkg) {
074            this.pkg = pkg;
075        }
076    
077        public void addImport(final ImportDef imp) {
078            assert imp != null;
079    
080            imports.add(imp);
081        }
082    
083        public Set getImports() {
084            return imports;
085        }
086    
087        public void addClass(final ClassDef def) {
088            assert def != null;
089    
090            def.setParent(this);
091            classes.add(def);
092        }
093    
094        public Set getClasses() {
095            return classes;
096        }
097    
098        public void addStatement(final Node node) {
099            assert node != null;
100    
101            statements.add(node);
102        }
103    
104        public List getStatements() {
105            return statements;
106        }
107    
108        public boolean hasStatements() {
109            return !statements.isEmpty();
110        }
111    
112        public String getScriptName() {
113            try {
114                // TODO: Use URL.toURI() once Java 5 is the base platform
115                File file = new File(new URI(url.toString()).getPath());
116                return FileUtils.basename(file.getName(), "." + FileUtils.extension(file.getName()));
117            }
118            catch (URISyntaxException e) {
119                throw new RuntimeException("Unable to determine script class name from: " + url, e);
120            }
121        }
122    }