Skip to content
Browse files

moved code to located ANTLR runtime source code to separate class and

added test to check whether locating this code works when the code is
contained in a JAR file
  • Loading branch information...
1 parent c15173d commit 3fa549ba475f2e2f106450c15b1145b9d24951c1 @mirkoseifert mirkoseifert committed Mar 31, 2013
View
33 ...k.codegen.antlr/src/org/emftext/sdk/codegen/antlr/creators/ANTLRPluginContentCreator.java
@@ -17,7 +17,7 @@
import java.io.File;
import java.io.IOException;
-import java.net.URL;
+import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
@@ -44,17 +44,17 @@
import org.emftext.sdk.concretesyntax.OptionTypes;
/**
- * This class creates the contents of the org.emftext.commons.antlr_version plug-in
- * by copying the ANTLR runtime classes from the org.emftext.sdk.antlr_version
- * plug-in. The creation of the commons plug-in can be disable using the syntax
- * option OptionTypes.OVERRIDE_ANTLR_PLUGIN.
+ * This class creates the contents of the org.emftext.commons.antlr_version
+ * plug-in by copying the ANTLR runtime classes from the
+ * org.emftext.sdk.antlr_version plug-in. The creation of the commons plug-in
+ * can be disable using the syntax option OptionTypes.OVERRIDE_ANTLR_PLUGIN.
*/
public class ANTLRPluginContentCreator {
private static final String SRC_FOLDER = "src";
/**
- * A list of all class to copy.
+ * A list of all classes to copy.
*/
private Class<?>[] antlrClassNames = new Class<?>[] {
org.antlr.runtime3_4_0.ANTLRFileStream.class,
@@ -187,21 +187,22 @@ public void generate(ANTLRGenerationContext context, IProgressMonitor monitor) t
exports.add(ANTLRPluginArtifacts.PACKAGE_ANTLR_RUNTIME_TREE.getPackage().getName(context));
manifestParameters.setPlugin(antlrPlugin);
- manifestParameters.setBundleName("ANTLR 3.3.0 Runtime Classes");
+ // TODO Use constant for ANTLR version
+ manifestParameters.setBundleName("ANTLR 3.4.0 Runtime Classes");
creators.add(new ManifestCreator<ANTLRGenerationContext>(manifestParameters, true));
+ Class<?> referenceClass = EMFTextSDKAntlrPlugin.class;
+ SourceCodeStreamFactory sourceCodeStreamFactory = new SourceCodeStreamFactory();
// add copiers for ANTLR source files
for (Class<?> antlrClass : antlrClassNames) {
- String relativePathSourceFile = antlrClass.getName().replace(".", "/") + ".java";
+ String className = antlrClass.getName();
+ InputStream sourceStream = sourceCodeStreamFactory.getSourceCodeStream(referenceClass,
+ className);
String pathFile = antlrClass.getName().replace(".", File.separator) + ".java";
- Class<EMFTextSDKAntlrPlugin> antlrPluginClass = EMFTextSDKAntlrPlugin.class;
- URL url = antlrPluginClass.getResource("");
- String packagePath = antlrPluginClass.getPackage().getName().replace(".", "/");
- String urlString = url.toString().replace("bin/" + packagePath + "/", "");
- urlString = urlString.replace(packagePath + "/", "");
- String pathToSourceFile = urlString + "/src-runtime/" + relativePathSourceFile;
- creators.add(new FileCopier<ANTLRGenerationContext>(new URL(pathToSourceFile).openStream(),
- new File(sourceFolderPath + pathFile), true));
+ File targetFile = new File(sourceFolderPath + pathFile);
+ FileCopier<ANTLRGenerationContext> fileCopier = new FileCopier<ANTLRGenerationContext>(sourceStream,
+ targetFile, true);
+ creators.add(fileCopier);
}
for (IArtifactCreator<ANTLRGenerationContext> creator : creators) {
View
1 Core/Tests/org.emftext.test/.classpath
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
+ <classpathentry exported="true" kind="lib" path="lib/SourceCodeStreamTestArchive.jar"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="output" path="bin"/>
View
2 Core/Tests/org.emftext.test/META-INF/MANIFEST.MF
@@ -47,4 +47,6 @@ Export-Package: org.emftext.sdk.codegen.composites,
org.emftext.test.resource,
org.emftext.test.syntax_analysis,
org.emftext.test.syntax_extension
+Bundle-ClassPath: lib/SourceCodeStreamTestArchive.jar,
+ .
View
3 Core/Tests/org.emftext.test/build.properties
@@ -1,4 +1,5 @@
source.. = src/
output.. = bin/
bin.includes = META-INF/,\
- .
+ .,\
+ lib/SourceCodeStreamTestArchive.jar
View
BIN Core/Tests/org.emftext.test/lib/SourceCodeStreamTestArchive.jar
Binary file not shown.
View
43 ....emftext.test/src/org/emftext/sdk/codegen/antlr/creators/SourceCodeStreamFactoryTest.java
@@ -0,0 +1,43 @@
+package org.emftext.sdk.codegen.antlr.creators;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.MalformedURLException;
+
+import org.antlr.runtime3_4_0.ANTLRFileStream;
+import org.emftext.sdk.util.StreamUtil;
+import org.junit.Test;
+
+/**
+ * The {@link SourceCodeStreamFactoryTest} checks whether the ANTLR runtime
+ * source files can be found in packaged JAR files. This is somewhat tricky,
+ * because the localization of the source code of these classes must work both
+ * when the EMFText plug-ins are present as projects (i.e., not packaged as JAR
+ * files) and when they're packaged (e.g., when using them in builds that rely
+ * on EMFText).
+ */
+public class SourceCodeStreamFactoryTest {
+
+ @Test
+ public void testGetSourceCodeStream() {
+ SourceCodeStreamFactory streamFactory = new SourceCodeStreamFactory();
+ try {
+ InputStream stream = streamFactory.getSourceCodeStream(SourceCodeStreamTestReferenceClass.class, ANTLRFileStream.class.getName());
+ assertNotNull("Stream expected", stream);
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ StreamUtil.copy(stream, output);
+ stream.close();
+ String sourceCode = output.toString();
+ assertTrue("Source code must contain class definition.", sourceCode.contains("public class ANTLRFileStream"));
+ } catch (MalformedURLException e) {
+ fail(e.getMessage());
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+ }
+}

0 comments on commit 3fa549b

Please sign in to comment.
Something went wrong with that request. Please try again.