|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ProjectFileParserFacade.java | 68.2% | 77.5% | 100% | 75.4% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.project; | |
| 38 | ||
| 39 | import java.io.*; | |
| 40 | import edu.rice.cs.drjava.config.OptionConstants; | |
| 41 | import edu.rice.cs.drjava.project.MalformedProjectFileException; | |
| 42 | ||
| 43 | /** Abstract project file parser. */ | |
| 44 | public class ProjectFileParserFacade { | |
| 45 | /** Singleton instance of ProjectFileParserFacade */ | |
| 46 | public static final ProjectFileParserFacade ONLY = new ProjectFileParserFacade(); | |
| 47 | 8 | protected ProjectFileParserFacade() { } |
| 48 | ||
| 49 | protected File _projectFile; | |
| 50 | protected boolean _xmlProjectFile; | |
| 51 | ||
| 52 | /** @param projFile the file to parse | |
| 53 | * @return the project file IR | |
| 54 | */ | |
| 55 | 8 | public ProjectFileIR parse(File projFile) throws IOException, FileNotFoundException, MalformedProjectFileException { |
| 56 | 8 | FileReader fr = new FileReader(projFile); |
| 57 | 8 | int read = fr.read(); |
| 58 | 8 | if (read==-1) { |
| 59 | // empty project file, throw exception | |
| 60 | 0 | throw new MalformedProjectFileException("Empty project file."); |
| 61 | } | |
| 62 | 8 | if (((char)read) != ';') { |
| 63 | // does not start with a ';', can't be an old S-expression format project file | |
| 64 | // try new XML format parser | |
| 65 | 3 | fr.close(); |
| 66 | 3 | return fixup(XMLProjectFileParser.ONLY.parse(projFile)); |
| 67 | } | |
| 68 | 5 | read = fr.read(); |
| 69 | 5 | if (read==-1) { |
| 70 | // project file just contained ";", throw exception | |
| 71 | 0 | throw new MalformedProjectFileException("Incomplete project file."); |
| 72 | } | |
| 73 | 5 | if (((char)read) != ';') { |
| 74 | // does not start with ";;", can't be an old S-expression format project file | |
| 75 | // try new XML format parser | |
| 76 | 0 | fr.close(); |
| 77 | 0 | return fixup(XMLProjectFileParser.ONLY.parse(projFile)); |
| 78 | } | |
| 79 | 5 | fr.close(); |
| 80 | // file started with ";;", try old S-expression format parser | |
| 81 | 5 | return fixup(ProjectFileParser.ONLY.parse(projFile)); |
| 82 | } | |
| 83 | ||
| 84 | private static edu.rice.cs.util.Log LOG = new edu.rice.cs.util.Log("ParserFacadeFixup.txt", false); | |
| 85 | ||
| 86 | /** | |
| 87 | * Here we check versions, and see if we need to apply a fixup to account for specify main-class as a classname instead of as a file. | |
| 88 | * All DrJava revisions before 4782 need to be fixed up. We also fixup all projects that have "unknown" versions. | |
| 89 | * | |
| 90 | * @param pfir - the ProjectProfile to fixup, if needed. | |
| 91 | */ | |
| 92 | 8 | protected ProjectFileIR fixup(ProjectFileIR pfir){ |
| 93 | 8 | boolean doFixup = false; |
| 94 | ||
| 95 | 8 | String version = pfir.getDrJavaVersion(); |
| 96 | ||
| 97 | 8 | if(version.equals("unknown")) |
| 98 | 5 | doFixup = true; |
| 99 | ||
| 100 | 8 | if(!doFixup){ |
| 101 | 3 | int i = version.indexOf("-r"); |
| 102 | ||
| 103 | 3 | if(i == -1){ |
| 104 | 0 | doFixup = true; |
| 105 | }else{ | |
| 106 | 3 | try{ |
| 107 | 3 | if(Integer.parseInt(version.substring(i+2).trim()) < 4782) |
| 108 | 0 | doFixup = true; |
| 109 | }catch(NumberFormatException e){ | |
| 110 | 0 | doFixup = true; |
| 111 | } | |
| 112 | } | |
| 113 | } | |
| 114 | ||
| 115 | 8 | LOG.log("DoFixup? " + doFixup); |
| 116 | ||
| 117 | 6 | if(!doFixup || pfir.getMainClass() == null) return pfir; |
| 118 | ||
| 119 | 2 | String mainClass = pfir.getMainClass(); |
| 120 | ||
| 121 | 2 | LOG.log("\tmainClass = \"" + mainClass + "\""); |
| 122 | ||
| 123 | 2 | String qualifiedName = mainClass; |
| 124 | ||
| 125 | //Strip off any leading slashes | |
| 126 | 2 | if(qualifiedName.startsWith("" + File.separatorChar)) |
| 127 | 0 | qualifiedName = qualifiedName.substring(1); |
| 128 | ||
| 129 | //Remove the .java extension if it exists | |
| 130 | 2 | if(qualifiedName.toLowerCase().endsWith(OptionConstants.JAVA_FILE_EXTENSION)) |
| 131 | 0 | qualifiedName = qualifiedName.substring(0, qualifiedName.length() - |
| 132 | OptionConstants.JAVA_FILE_EXTENSION.length()); | |
| 133 | ||
| 134 | 2 | LOG.log("\tsetMainClass = \"" + qualifiedName + "\""); |
| 135 | ||
| 136 | //Replace path seperators with java standard '.' package seperators. | |
| 137 | 2 | pfir.setMainClass(qualifiedName.replace(File.separatorChar, '.')); |
| 138 | ||
| 139 | 2 | return pfir; |
| 140 | } | |
| 141 | } |
|
||||||||||