org.eclipse.xtext.serializer.ISerializer Java Examples

The following examples show how to use org.eclipse.xtext.serializer.ISerializer. 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: JSONModelUtils.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Serializes the given {@link JSONDocument} using the Xtext serialization facilities provided by the JSON language.
 */
public static String serializeJSON(JSONDocument document) {
	ISerializer jsonSerializer = N4LanguageUtils.getServiceForContext(FILE_EXTENSION, ISerializer.class).get();
	ResourceSet resourceSet = N4LanguageUtils.getServiceForContext(FILE_EXTENSION, ResourceSet.class).get();

	// Use temporary Resource as AbstractFormatter2 implementations can only format
	// semantic elements that are contained in a Resource.
	Resource temporaryResource = resourceSet.createResource(URIUtils.toFileUri("__synthetic." + FILE_EXTENSION));
	temporaryResource.getContents().add(document);

	// create string writer as serialization output
	StringWriter writer = new StringWriter();

	// enable formatting as serialization option
	SaveOptions serializerOptions = SaveOptions.newBuilder().format().getOptions();
	try {
		jsonSerializer.serialize(document, writer, serializerOptions);
		return writer.toString();
	} catch (IOException e) {
		throw new RuntimeException("Failed to serialize JSONDocument " + document, e);
	}
}
 
Example #2
Source File: SerializerTest.java    From xtext-extras with Eclipse Public License 2.0 6 votes vote down vote up
@Test public void testSerialize_02() throws Exception {
	Resource resource = newResource("'foo' as String");
	XCastedExpression casted = (XCastedExpression) resource.getContents().get(0);
	
	XbaseFactory factory = XbaseFactory.eINSTANCE;
	XIfExpression ifExpression = factory.createXIfExpression();
	ifExpression.setIf(factory.createXBooleanLiteral());
	XStringLiteral stringLiteral = factory.createXStringLiteral();
	stringLiteral.setValue("value");
	ifExpression.setThen(stringLiteral);
	XInstanceOfExpression instanceOfExpression = factory.createXInstanceOfExpression();
	instanceOfExpression.setExpression(ifExpression);
	instanceOfExpression.setType(EcoreUtil.copy(casted.getType()));
	resource.getContents().clear();
	resource.getContents().add(instanceOfExpression);
	ISerializer serializer = get(ISerializer.class);
	String string = serializer.serialize(instanceOfExpression);
	// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=464846
	assertEquals("( if(false) \"value\" ) instanceof String", string);
	
	XInstanceOfExpression parsedExpression = parseHelper.parse(string);
	assertTrue(EcoreUtil.equals(instanceOfExpression, parsedExpression));
}
 
Example #3
Source File: SerializerFragment.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public Set<Binding> getGuiceBindingsRt(Grammar grammar) {
	BindFactory bf = new BindFactory();
	bf.addTypeToType(ISemanticSequencer.class.getName(), names.getSemanticSequencer().getQualifiedName());
	bf.addTypeToType(ISyntacticSequencer.class.getName(), names.getSyntacticSequencer().getQualifiedName());
	bf.addTypeToType(ISerializer.class.getName(), Serializer.class.getName());
	return bf.getBindings();
}
 
Example #4
Source File: SerializerFragment2.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public void generate() {
  new GuiceModuleAccess.BindingFactory().addTypeToType(TypeReference.typeRef(ISemanticSequencer.class), this.getSemanticSequencerClass(this.getGrammar())).addTypeToType(TypeReference.typeRef(ISyntacticSequencer.class), this.getSyntacticSequencerClass(this.getGrammar())).addTypeToType(TypeReference.typeRef(ISerializer.class), TypeReference.typeRef(Serializer.class)).contributeTo(this.getLanguage().getRuntimeGenModule());
  ManifestAccess _manifest = this.getProjectConfig().getRuntime().getManifest();
  boolean _tripleNotEquals = (_manifest != null);
  if (_tripleNotEquals) {
    Set<String> _exportedPackages = this.getProjectConfig().getRuntime().getManifest().getExportedPackages();
    String _serializerBasePackage = this.getSerializerBasePackage(this.getGrammar());
    _exportedPackages.add(_serializerBasePackage);
    Set<String> _requiredBundles = this.getProjectConfig().getRuntime().getManifest().getRequiredBundles();
    String _xbaseLibVersionLowerBound = this.getProjectConfig().getRuntime().getXbaseLibVersionLowerBound();
    String _plus = ("org.eclipse.xtext.xbase.lib;bundle-version=\"" + _xbaseLibVersionLowerBound);
    String _plus_1 = (_plus + "\"");
    _requiredBundles.add(_plus_1);
  }
  this.generateAbstractSemanticSequencer();
  this.generateAbstractSyntacticSequencer();
  boolean _isGenerateStub = this.isGenerateStub();
  if (_isGenerateStub) {
    this.generateSemanticSequencer();
    this.generateSyntacticSequencer();
  }
  if (this.generateDebugData) {
    this.generateGrammarConstraints();
    Iterable<Pair<String, String>> _generateDebugGraphs = this.debugGraphGenerator.generateDebugGraphs();
    for (final Pair<String, String> fileToContent : _generateDebugGraphs) {
      this.getProjectConfig().getRuntime().getSrcGen().generateFile(fileToContent.getKey(), fileToContent.getValue());
    }
  }
}
 
Example #5
Source File: GrammarAccessExtensions.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
private ISerializer getSerializer() {
  final String delimiter = this.codeConfig.getLineDelimiter();
  ISerializer result = this.xtextSerializerByLineDelimiter.get(delimiter);
  if ((result != null)) {
    return result;
  }
  final ILineSeparatorInformation _function = () -> {
    return delimiter;
  };
  GrammarAccessExtensions.LineSeparatorModule _lineSeparatorModule = new GrammarAccessExtensions.LineSeparatorModule(_function);
  final Injector injector = Guice.createInjector(_lineSeparatorModule);
  result = injector.<ISerializer>getInstance(ISerializer.class);
  this.xtextSerializerByLineDelimiter.put(delimiter, result);
  return result;
}
 
Example #6
Source File: AbstractLazyLinkingTestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #7
Source File: AbstractBug303200TestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #8
Source File: AbstractBug288760TestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #9
Source File: AbstractBacktrackingLexerTestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #10
Source File: AbstractKeywordsTestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #11
Source File: AbstractAbstractTestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #12
Source File: AbstractBug311337TestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #13
Source File: XtextRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * @since 2.9
 */
@Override
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #14
Source File: AbstractBug381381TestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #15
Source File: AbstractRefactoringTestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #16
Source File: AbstractGH341TestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #17
Source File: AbstractEncodingUiTestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #18
Source File: AbstractInheritanceTest3LanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #19
Source File: AbstractTestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #20
Source File: AbstractBug307519TestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #21
Source File: AbstractEObjectAtOffsetTestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #22
Source File: AbstractIndentationAwareTestLanguageRuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #23
Source File: AbstractBug288734TestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #24
Source File: AbstractDomainModelTestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #25
Source File: AbstractBug309949TestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #26
Source File: AbstractParametersTestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #27
Source File: AbstractOutlineTestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #28
Source File: AbstractBug362902RuntimeModule.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #29
Source File: AbstractEnumRulesUiTestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}
 
Example #30
Source File: AbstractContentAssistTestLanguageRuntimeModule.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public Class<? extends ISerializer> bindISerializer() {
	return Serializer.class;
}