org.eclipse.xtext.generator.IFileSystemAccess2 Java Examples

The following examples show how to use org.eclipse.xtext.generator.IFileSystemAccess2. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PyGenerator.java    From sarl with Apache License 2.0 6 votes vote down vote up
/** Generate the Python package files.
 *
 * <p>This function generates the "__init__.py" files for all the packages.
 *
 * @param name the name of the generated type.
 * @param lineSeparator the line separator.
 * @param context the generation context.
 */
protected void writePackageFiles(QualifiedName name, String lineSeparator,
		IExtraLanguageGeneratorContext context) {
	final IFileSystemAccess2 fsa = context.getFileSystemAccess();
	final String outputConfiguration = getOutputConfigurationName();
	QualifiedName libraryName = null;
	for (final String segment : name.skipLast(1).getSegments()) {
		if (libraryName == null) {
			libraryName = QualifiedName.create(segment, LIBRARY_FILENAME);
		} else {
			libraryName = libraryName.append(segment).append(LIBRARY_FILENAME);
		}
		final String fileName = toFilename(libraryName);
		if (!fsa.isFile(fileName)) {
			final String content = PYTHON_FILE_HEADER + lineSeparator
					+ getGenerationComment(context) + lineSeparator
					+ LIBRARY_CONTENT;
			if (Strings.isEmpty(outputConfiguration)) {
				fsa.generateFile(fileName, content);
			} else {
				fsa.generateFile(fileName, outputConfiguration, content);
			}
		}
		libraryName = libraryName.skipLast(1);
	}
}
 
Example #2
Source File: ManifestAccess.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void writeTo(final IFileSystemAccess2 fileSystemAccess) {
  try {
    if ((fileSystemAccess != null)) {
      CharSequence _content = this.getContent();
      StringBuffer _stringBuffer = new StringBuffer(_content);
      final String contentToWrite = MergeableManifest2.make512Safe(_stringBuffer, this.lineDelimiter);
      byte[] _bytes = contentToWrite.getBytes("UTF-8");
      ByteArrayInputStream _byteArrayInputStream = new ByteArrayInputStream(_bytes);
      final MergeableManifest2 mergeableManifest = new MergeableManifest2(_byteArrayInputStream);
      mergeableManifest.setLineDelimiter(this.lineDelimiter);
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      mergeableManifest.write(bout);
      String _path = this.getPath();
      byte[] _byteArray = bout.toByteArray();
      ByteArrayInputStream _byteArrayInputStream_1 = new ByteArrayInputStream(_byteArray);
      fileSystemAccess.generateFile(_path, _byteArrayInputStream_1);
    }
  } catch (Throwable _e) {
    throw Exceptions.sneakyThrow(_e);
  }
}
 
Example #3
Source File: IndexTestLanguageGenerator.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
	TreeIterator<EObject> iter = input.getAllContents();
	while (iter.hasNext()) {
		EObject e = iter.next();
		if (e instanceof Entity) {
			Entity entity = (Entity) e;
			StringConcatenation builder = new StringConcatenation();
			builder.append("Hello ");
			builder.append(entity.getName());
			builder.append("!");
			builder.newLineIfNotEmpty();
			fsa.generateFile(entity.getName() + ".txt", builder);
		}
	}
}
 
Example #4
Source File: ExtraLanguageGeneratorSupport.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
	final IGenerator2 mainGenerator = getMainGenerator();
	if (mainGenerator != null) {
		mainGenerator.doGenerate(input, fsa, context);
	}
	final Iterable<IRootGenerator> generators = getExtraGeneratorProvider().getGenerators(context, input);
	if (generators != null) {
		for (final IGenerator2 generator : generators) {
			try {
				generator.doGenerate(input, fsa, context);
			} catch (Throwable exception) {
				getLogger().log(Level.SEVERE, exception.getLocalizedMessage(), exception);
			}
		}
	}
}
 
Example #5
Source File: ExtraLanguageGeneratorSupport.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
	final IGenerator2 mainGenerator = getMainGenerator();
	if (mainGenerator != null) {
		mainGenerator.beforeGenerate(input, fsa, context);
	}
	final Iterable<IRootGenerator> generators = getExtraGeneratorProvider().getGenerators(context, input);
	if (generators != null) {
		for (final IGenerator2 generator : generators) {
			try {
				generator.beforeGenerate(input, fsa, context);
			} catch (Throwable exception) {
				getLogger().log(Level.SEVERE, exception.getLocalizedMessage(), exception);
			}
		}
	}
}
 
Example #6
Source File: ExtraLanguageGeneratorSupport.java    From sarl with Apache License 2.0 6 votes vote down vote up
@Override
public void afterGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
	final IGenerator2 mainGenerator = getMainGenerator();
	if (mainGenerator != null) {
		mainGenerator.afterGenerate(input, fsa, context);
	}
	final Iterable<IRootGenerator> generators = getExtraGeneratorProvider().getGenerators(context, input);
	if (generators != null) {
		for (final IGenerator2 generator : generators) {
			try {
				generator.afterGenerate(input, fsa, context);
			} catch (Throwable exception) {
				getLogger().log(Level.SEVERE, exception.getLocalizedMessage(), exception);
			}
		}
	}
}
 
Example #7
Source File: MyGenerator.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess fsa) {
	TreeIterator<EObject> allContents = input.getAllContents();
	while (allContents.hasNext()) {
		EObject next = allContents.next();
		if (next instanceof Element) {
			Element ele = (Element) next;
			String fileName = ele.getName() + ".txt";
			if (fsa instanceof IFileSystemAccess2) {
				IFileSystemAccess2 fileSystemAccess2 = (IFileSystemAccess2) fsa;
				if (fileSystemAccess2.isFile(fileName)) {
					fileSystemAccess2.readTextFile(fileName);
				}
			}
			fsa.generateFile(fileName, "object " + ele.getName());
		}
	}
}
 
Example #8
Source File: AbstractSubCodeBuilderFragment.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Binds the given references according to the standard policy.
 *
 * <p>If an custom implementation is defined, it is binded to. Otherwise, the default implementation
 * is binded.
 *
 * @param factory the binding factory to use for creating the bindings.
 * @param interfaceType the type to bind to an implementation type.
 * @param implementationType the implementation to bind to the interface type.
 * @param customImplementationType the custom implementation to bind to the interface type.
 */
protected void bindTypeReferences(BindingFactory factory, TypeReference interfaceType,
		TypeReference implementationType, TypeReference customImplementationType) {
	final IFileSystemAccess2 fileSystem = getSrc();
	final TypeReference type;
	if ((fileSystem.isFile(implementationType.getJavaPath()))
			|| (fileSystem.isFile(customImplementationType.getXtendPath()))) {
		type = customImplementationType;
	} else {
		type = implementationType;
	}
	factory.addfinalTypeToType(interfaceType, type);
}
 
Example #9
Source File: FileAwareTestLanguageGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
	public void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
//		Iterator<Greeting> filtered = Iterators.filter(resource.getAllContents(), Greeting.class);
//		Iterator<String> names = Iterators.transform(filtered, new Function<Greeting, String>() {
//
//			@Override
//			public String apply(Greeting greeting) {
//				return greeting.getName();
//			}
//		});
//		fsa.generateFile("greetings.txt", "People to greet: " + IteratorExtensions.join(names, ", "));
	}
 
Example #10
Source File: PluginXmlAccess.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void writeTo(final IFileSystemAccess2 fileSystemAccess) {
  boolean _isEmpty = this.entries.isEmpty();
  boolean _not = (!_isEmpty);
  if (_not) {
    super.writeTo(fileSystemAccess);
  }
}
 
Example #11
Source File: NoJdtTestLanguageGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
	Iterator<Greeting> filtered = Iterators.filter(resource.getAllContents(), Greeting.class);
	Iterator<String> names = Iterators.transform(filtered, new Function<Greeting, String>() {

		@Override
		public String apply(Greeting greeting) {
			return greeting.getName();
		}
	});
	String fileName = resource.getURI().lastSegment();
	if(fileName == null) fileName = "greetings";
	fsa.generateFile(fileName+".txt", "People to greet: " + IteratorExtensions.join(names, ", "));
}
 
Example #12
Source File: NestedRefsTestLanguageGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
	public void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
//		Iterator<Greeting> filtered = Iterators.filter(resource.getAllContents(), Greeting.class);
//		Iterator<String> names = Iterators.transform(filtered, new Function<Greeting, String>() {
//
//			@Override
//			public String apply(Greeting greeting) {
//				return greeting.getName();
//			}
//		});
//		fsa.generateFile("greetings.txt", "People to greet: " + IteratorExtensions.join(names, ", "));
	}
 
Example #13
Source File: AbstractExtraLanguageGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Create the generator context for this generator.
 *
 * @param fsa the file system access.
 * @param context the global context.
 * @param resource the resource.
 * @return the context.
 */
protected IExtraLanguageGeneratorContext createGeneratorContext(IFileSystemAccess2 fsa, IGeneratorContext context,
		Resource resource) {
	if (context instanceof IExtraLanguageGeneratorContext) {
		return (IExtraLanguageGeneratorContext) context;
	}
	return new ExtraLanguageGeneratorContext(context, fsa, this, resource, getPreferenceID());
}
 
Example #14
Source File: GamlGenerator.java    From gama with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param input
 * @return
 */
// private String getFilenameFor(final GamlResource input) {
// IPath path = GamlResourceServices.getPathOf(input);
// final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
// path = file.getProjectRelativePath();
// final String s = path.toString();
// return s + ".meta";
// }

@Override
public void doGenerate(final Resource input, final IFileSystemAccess2 fsa, final IGeneratorContext context) {
	// TODO Auto-generated method stub

}
 
Example #15
Source File: BuilderFactoryFragment.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void generateXtendStubs() {
	super.generateXtendStubs();
	final TypeReference stub = getBuilderFactoryImplCustom();
	final StringConcatenationClient content = new StringConcatenationClient() {
		@Override
		protected void appendTo(TargetStringConcatenation it) {
			it.append("/** User-defined builder factory of the " + getLanguageName() //$NON-NLS-1$
					+ " scripts."); //$NON-NLS-1$
			it.newLine();
			it.append(" */"); //$NON-NLS-1$
			it.newLine();
			it.append("class "); //$NON-NLS-1$
			it.append(stub);
			it.append(" extends "); //$NON-NLS-1$
			it.append(getBuilderFactoryImpl());
			it.append(" {"); //$NON-NLS-1$
			it.newLineIfNotEmpty();
			it.newLine();
			it.append("}"); //$NON-NLS-1$
			it.newLine();
		}

	};
	final XtendFileAccess xtendFile = getFileAccessFactory().createXtendFile(stub, content);
	final IFileSystemAccess2 fileSystem = getSrc();
	if (!fileSystem.isFile(xtendFile.getPath())) {
		xtendFile.writeTo(fileSystem);
	}
}
 
Example #16
Source File: BuilderFactoryFragment.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("checkstyle:all")
public void generateJavaStubs() {
	super.generateJavaStubs();
	final TypeReference stub = getBuilderFactoryImplCustom();
	StringConcatenationClient content = new StringConcatenationClient() {
		@Override
		protected void appendTo(TargetStringConcatenation it) {
			it.append("/** User-defined builder factory of the " + getLanguageName() + " scripts."); //$NON-NLS-1$//$NON-NLS-2$
			it.newLine();
			it.append(" */"); //$NON-NLS-1$
			it.newLine();
			it.append("public class "); //$NON-NLS-1$
			it.append(stub);
			it.append(" extends "); //$NON-NLS-1$
			it.append(getBuilderFactoryImpl());
			it.append(" {"); //$NON-NLS-1$
			it.newLineIfNotEmpty();
			it.newLine();
			it.append("}"); //$NON-NLS-1$
			it.newLine();
		}

	};
	JavaFileAccess javaFile = getFileAccessFactory().createJavaFile(stub, content);
	IFileSystemAccess2 fileSystem = getSrc();
	if (!fileSystem.isFile(javaFile.getPath())) {
		javaFile.writeTo(fileSystem);
	}
}
 
Example #17
Source File: BuilderFactoryFragment.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void generateRuntimeBindings(BindingFactory factory) {
	super.generateRuntimeBindings(factory);
	final IFileSystemAccess2 fileSystem = getSrc();
	final TypeReference type;
	if ((fileSystem.isFile(getBuilderFactoryImplCustom().getJavaPath()))
			|| (fileSystem.isFile(getBuilderFactoryImplCustom().getXtendPath()))) {
		type = getBuilderFactoryImplCustom();
	} else {
		type = getBuilderFactoryImpl();
	}
	factory.addfinalTypeToType(getBuilderFactoryImpl(), type);
}
 
Example #18
Source File: AbstractExtraLanguageGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
	final IExtraLanguageGeneratorContext generatorContext = createGeneratorContext(fsa, context, input);
	final EList<EObject> contents = input.getContents();
	for (final EObject obj : contents) {
		if (canGenerateFor(obj)) {
			before(obj, generatorContext);
			final Iterator<EObject> iterator = EcoreUtil.getAllContents(obj, false);
			while (iterator.hasNext()) {
				final EObject subobj = iterator.next();
				before(subobj, generatorContext);
			}
		}
	}
}
 
Example #19
Source File: ScriptBuilderFragment.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void generateXtendStubs() {
	super.generateXtendStubs();
	final TypeReference stub = getScriptBuilderImplCustom();
	final StringConcatenationClient content = new StringConcatenationClient() {
		@Override
		protected void appendTo(TargetStringConcatenation it) {
			it.append("/** User-defined builder of the " + getLanguageName() + " scripts."); //$NON-NLS-1$//$NON-NLS-2$
			it.newLine();
			it.append(" */"); //$NON-NLS-1$
			it.newLine();
			it.append("class "); //$NON-NLS-1$
			it.append(stub);
			it.append(" extends "); //$NON-NLS-1$
			it.append(getScriptBuilderImpl());
			it.append(" {"); //$NON-NLS-1$
			it.newLineIfNotEmpty();
			it.newLine();
			it.append("}"); //$NON-NLS-1$
			it.newLine();
		}

	};
	final XtendFileAccess xtendFile = getFileAccessFactory().createXtendFile(stub, content);
	final IFileSystemAccess2 fileSystem = getSrc();
	if (!fileSystem.isFile(xtendFile.getPath())) {
		xtendFile.writeTo(fileSystem);
	}
}
 
Example #20
Source File: ScriptBuilderFragment.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void generateJavaStubs() {
	super.generateJavaStubs();
	final TypeReference stub = getScriptBuilderImplCustom();
	final StringConcatenationClient content = new StringConcatenationClient() {
		@Override
		protected void appendTo(TargetStringConcatenation it) {
			it.append("/** User-defined builder of the " + getLanguageName() + " scripts."); //$NON-NLS-1$//$NON-NLS-2$
			it.newLine();
			it.append(" */"); //$NON-NLS-1$
			it.newLine();
			it.append("public class "); //$NON-NLS-1$
			it.append(stub);
			it.append(" extends "); //$NON-NLS-1$
			it.append(getScriptBuilderImpl());
			it.append(" {"); //$NON-NLS-1$
			it.newLineIfNotEmpty();
			it.newLine();
			it.append("}"); //$NON-NLS-1$
			it.newLine();
		}

	};
	final JavaFileAccess javaFile = getFileAccessFactory().createJavaFile(stub, content);
	final IFileSystemAccess2 fileSystem = getSrc();
	if (!fileSystem.isFile(javaFile.getPath())) {
		javaFile.writeTo(fileSystem);
	}
}
 
Example #21
Source File: ExtraLanguageGeneratorSupport.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Generate the file(s) from the input resource.
 *
 * @param input the input resource.
 * @param fsa the file system access.
 * @param context the generator context.
 */
public final void generate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
	try {
		beforeGenerate(input, fsa, context);
		doGenerate(input, fsa, context);
	} finally {
		afterGenerate(input, fsa, context);
	}
}
 
Example #22
Source File: ExtraLanguageGeneratorSupport.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public final void doGenerate(Resource input, IFileSystemAccess fsa) {
	final IFileSystemAccess2 casted = (IFileSystemAccess2) fsa;
	final GeneratorContext context = new GeneratorContext();
	try {
		beforeGenerate(input, casted, context);
		doGenerate(input, casted, context);
	} finally {
		afterGenerate(input, casted, context);
	}
}
 
Example #23
Source File: AbstractExtraLanguageGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void afterGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
	final IExtraLanguageGeneratorContext generatorContext = createGeneratorContext(fsa, context, input);
	final EList<EObject> contents = input.getContents();
	for (final EObject obj : contents) {
		if (canGenerateFor(obj)) {
			final Iterator<EObject> iterator = EcoreUtil.getAllContents(obj, false);
			while (iterator.hasNext()) {
				final EObject subobj = iterator.next();
				after(subobj, generatorContext);
			}
			after(obj, generatorContext);
		}
	}
}
 
Example #24
Source File: AbstractExtraLanguageGenerator.java    From sarl with Apache License 2.0 5 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess2 fsa, IGeneratorContext context) {
	final IExtraLanguageGeneratorContext generatorContext = createGeneratorContext(fsa, context, input);
	initializeContext(generatorContext);
	final EList<EObject> contents = input.getContents();
	for (final EObject obj : contents) {
		if (canGenerateFor(obj)) {
			generate(obj, generatorContext);
		}
	}
}
 
Example #25
Source File: ExtraLanguageGeneratorContext.java    From sarl with Apache License 2.0 5 votes vote down vote up
/** Create the context for the given delegate.
 *
 * @param delegate the delegate.
 * @param fileSystemAccess the file system access.
 * @param generator the root generator.
 * @param resource the resource.
 * @param preferenceId the identifier of the container of the generator's preferences.
 */
public ExtraLanguageGeneratorContext(IGeneratorContext delegate, IFileSystemAccess2 fileSystemAccess,
		IRootGenerator generator, Resource resource, String preferenceId) {
	this.identifier = UUID.randomUUID();
	this.generationDate = new Date();
	this.preferenceId = preferenceId;
	this.delegate = delegate;
	this.fileSystemAccess = fileSystemAccess;
	this.resource = resource;
	this.rootGenerator = new WeakReference<>(generator);
}
 
Example #26
Source File: MyGenerator.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerate(Resource input, IFileSystemAccess fsa) {
	for (Element ele : Iterables.filter(IteratorExtensions.toIterable(input.getAllContents()),
			Element.class)) {
		String fileName = ele.getName() + ".txt";
		if (fsa instanceof IFileSystemAccess2) {
			if (((IFileSystemAccess2) fsa).isFile(fileName)) {
				((IFileSystemAccess2) fsa).readTextFile(fileName);
			}
		}
		fsa.generateFile(fileName, "object " + ele.getName());
	}
}
 
Example #27
Source File: FragmentAdapter.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
protected String getPath(final IFileSystemAccess2 fsa) {
  final String path = fsa.getURI("").toFileString();
  boolean _endsWith = path.endsWith(File.separator);
  if (_endsWith) {
    int _length = path.length();
    int _minus = (_length - 1);
    return path.substring(0, _minus);
  } else {
    return path;
  }
}
 
Example #28
Source File: StatemachineGenerator.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void doGenerate(final Resource resource, final IFileSystemAccess2 fsa, final IGeneratorContext context) {
  String _className = this.className(resource);
  String _plus = (_className + ".java");
  EObject _head = IterableExtensions.<EObject>head(resource.getContents());
  fsa.generateFile(_plus, this.toJavaCode(((Statemachine) _head)));
}
 
Example #29
Source File: JavaProjectBasedBuilderParticipant.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
protected void handleChangedContents(Delta delta, IBuildContext context, IFileSystemAccess fileSystemAccess) {
	if (!resourceServiceProvider.canHandle(delta.getUri()))
		return;
	Resource resource = context.getResourceSet().getResource(delta.getUri(), true);
	if (shouldGenerate(resource, context)) {
		CancelIndicator cancelIndicator = CancelIndicator.NullImpl;
		if (fileSystemAccess instanceof EclipseResourceFileSystemAccess2) {
			cancelIndicator = new MonitorBasedCancelIndicator(((EclipseResourceFileSystemAccess2) fileSystemAccess).getMonitor());
		}
		GeneratorContext generatorContext = new GeneratorContext();
		generatorContext.setCancelIndicator(cancelIndicator);
		generator.generate(resource, (IFileSystemAccess2) fileSystemAccess, generatorContext);
		context.needRebuild();
	}
}
 
Example #30
Source File: PartialSerializationTestLanguageGenerator.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
	public void doGenerate(Resource resource, IFileSystemAccess2 fsa, IGeneratorContext context) {
//		Iterator<Greeting> filtered = Iterators.filter(resource.getAllContents(), Greeting.class);
//		Iterator<String> names = Iterators.transform(filtered, new Function<Greeting, String>() {
//
//			@Override
//			public String apply(Greeting greeting) {
//				return greeting.getName();
//			}
//		});
//		fsa.generateFile("greetings.txt", "People to greet: " + IteratorExtensions.join(names, ", "));
	}