001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
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.jetbrains.kotlin.cli.common.modules;
018    
019    import com.intellij.openapi.util.io.StreamUtil;
020    import com.intellij.util.SmartList;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.kotlin.cli.common.messages.MessageCollector;
023    import org.jetbrains.kotlin.cli.common.messages.MessageCollectorUtil;
024    import org.jetbrains.kotlin.cli.common.messages.OutputMessageUtil;
025    import org.jetbrains.kotlin.modules.Module;
026    import org.xml.sax.Attributes;
027    import org.xml.sax.SAXException;
028    import org.xml.sax.helpers.DefaultHandler;
029    
030    import javax.xml.parsers.ParserConfigurationException;
031    import javax.xml.parsers.SAXParser;
032    import javax.xml.parsers.SAXParserFactory;
033    import java.io.*;
034    import java.util.List;
035    
036    import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageLocation.NO_LOCATION;
037    import static org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity.ERROR;
038    
039    public class ModuleXmlParser {
040    
041        public static final String MODULES = "modules";
042        public static final String MODULE = "module";
043        public static final String NAME = "name";
044        public static final String TYPE = "type";
045        public static final String TYPE_PRODUCTION = "java-production";
046        public static final String TYPE_TEST = "java-test";
047        public static final String OUTPUT_DIR = "outputDir";
048        public static final String SOURCES = "sources";
049        public static final String JAVA_SOURCE_ROOTS = "javaSourceRoots";
050        public static final String PATH = "path";
051        public static final String CLASSPATH = "classpath";
052    
053        @NotNull
054        public static ModuleScriptData parseModuleScript(
055                @NotNull String xmlFile,
056                @NotNull MessageCollector messageCollector
057        ) {
058            FileInputStream stream = null;
059            try {
060                //noinspection IOResourceOpenedButNotSafelyClosed
061                stream = new FileInputStream(xmlFile);
062                return new ModuleXmlParser(messageCollector).parse(new BufferedInputStream(stream));
063            }
064            catch (FileNotFoundException e) {
065                MessageCollectorUtil.reportException(messageCollector, e);
066                return ModuleScriptData.EMPTY;
067            }
068            finally {
069                StreamUtil.closeStream(stream);
070            }
071        }
072    
073        private final MessageCollector messageCollector;
074        private final List<Module> modules = new SmartList<Module>();
075        private DefaultHandler currentState;
076    
077        private ModuleXmlParser(@NotNull MessageCollector messageCollector) {
078            this.messageCollector = messageCollector;
079        }
080    
081        private void setCurrentState(@NotNull DefaultHandler currentState) {
082            this.currentState = currentState;
083        }
084    
085        private ModuleScriptData parse(@NotNull InputStream xml) {
086            try {
087                setCurrentState(initial);
088                SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
089                saxParser.parse(xml, new DelegatedSaxHandler() {
090                    @NotNull
091                    @Override
092                    protected DefaultHandler getDelegate() {
093                        return currentState;
094                    }
095                });
096                return new ModuleScriptData(modules);
097            }
098            catch (ParserConfigurationException e) {
099                MessageCollectorUtil.reportException(messageCollector, e);
100            }
101            catch (SAXException e) {
102                messageCollector.report(ERROR, OutputMessageUtil.renderException(e), NO_LOCATION);
103            }
104            catch (IOException e) {
105                MessageCollectorUtil.reportException(messageCollector, e);
106            }
107            return ModuleScriptData.EMPTY;
108        }
109    
110        private final DefaultHandler initial = new DefaultHandler() {
111            @Override
112            public void startElement(@NotNull String uri, @NotNull String localName, @NotNull String qName, @NotNull Attributes attributes)
113                    throws SAXException {
114                if (!MODULES.equalsIgnoreCase(qName)) {
115                    throw createError(qName);
116                }
117    
118                setCurrentState(insideModules);
119            }
120        };
121    
122        private final DefaultHandler insideModules = new DefaultHandler() {
123            @Override
124            public void startElement(@NotNull String uri, @NotNull String localName, @NotNull String qName, @NotNull Attributes attributes)
125                    throws SAXException {
126                if (!MODULE.equalsIgnoreCase(qName)) {
127                    throw createError(qName);
128                }
129    
130                String moduleType = getAttribute(attributes, TYPE, qName);
131                assert(TYPE_PRODUCTION.equals(moduleType) || TYPE_TEST.equals(moduleType)): "Unknown module type: " + moduleType;
132                setCurrentState(new InsideModule(
133                        getAttribute(attributes, NAME, qName),
134                        getAttribute(attributes, OUTPUT_DIR, qName),
135                        moduleType
136                ));
137            }
138    
139            @Override
140            public void endElement(String uri, @NotNull String localName, @NotNull String qName) throws SAXException {
141                if (MODULE.equalsIgnoreCase(qName) || MODULES.equalsIgnoreCase(qName)) {
142                    setCurrentState(insideModules);
143                }
144            }
145        };
146    
147        private class InsideModule extends DefaultHandler {
148    
149            private final ModuleBuilder moduleBuilder;
150            private InsideModule(String name, String outputDir, @NotNull String type) {
151                this.moduleBuilder = new ModuleBuilder(name, outputDir, type);
152                modules.add(moduleBuilder);
153            }
154    
155            @Override
156            public void startElement(@NotNull String uri, @NotNull String localName, @NotNull String qName, @NotNull Attributes attributes)
157                    throws SAXException {
158                if (SOURCES.equalsIgnoreCase(qName)) {
159                    String path = getAttribute(attributes, PATH, qName);
160                    moduleBuilder.addSourceFiles(path);
161                }
162                else if (CLASSPATH.equalsIgnoreCase(qName)) {
163                    String path = getAttribute(attributes, PATH, qName);
164                    moduleBuilder.addClasspathEntry(path);
165                }
166                else if (JAVA_SOURCE_ROOTS.equalsIgnoreCase(qName)) {
167                    String path = getAttribute(attributes, PATH, qName);
168                    moduleBuilder.addJavaSourceRoot(path);
169                }
170                else {
171                    throw createError(qName);
172                }
173            }
174    
175            @Override
176            public void endElement(String uri, @NotNull String localName, @NotNull String qName) throws SAXException {
177                if (MODULE.equalsIgnoreCase(qName)) {
178                    setCurrentState(insideModules);
179                }
180            }
181        }
182    
183        @NotNull
184        private static String getAttribute(Attributes attributes, String qName, String tag) throws SAXException {
185            String name = attributes.getValue(qName);
186            if (name == null) {
187                throw new SAXException("No '" + qName + "' attribute for " + tag);
188            }
189            return name;
190        }
191    
192        private static SAXException createError(String qName) throws SAXException {
193            return new SAXException("Unexpected tag: " + qName);
194        }
195    }