@Override public String serialize(EObject obj, SaveOptions options) { checkNotNull(obj, "obj must not be null."); checkNotNull(options, "options must not be null."); try { if (formatter2Provider != null) { StringBuilder builder = new StringBuilder(); serialize(obj, builder, options); return builder.toString(); } else { TokenStringBuffer tokenStringBuffer = new TokenStringBuffer(); serialize(obj, tokenStringBuffer, options); return tokenStringBuffer.toString(); } } catch (IOException e) { throw new RuntimeException(e); } }
@Override public IFormattedRegion format(ICompositeNode root, int offset, int length) { String indent = getIndentation(root, offset); TokenStringBuffer buf = new TokenStringBuffer(); ITokenStream out = offset == 0 ? buf : new FilterFirstWhitespaceStream(buf); ITokenStream fmt; if (formatter instanceof IFormatterExtension) { EObject semanticElement = NodeModelUtils.findActualSemanticObjectFor(root); if (semanticElement != null) fmt = ((IFormatterExtension) formatter).createFormatterStream(semanticElement, indent, out, false); else { // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=380406 ITextRegion rootRegion = root.getTextRegion(); return new FormattedRegion(rootRegion.getOffset(), rootRegion.getLength(), root.getText()); } } else fmt = formatter.createFormatterStream(indent, out, false); try { ITextRegion range = nodeModelStreamer.feedTokenStream(fmt, root, offset, length); return new FormattedRegion(range.getOffset(), range.getLength(), buf.toString()); } catch (IOException e) { // this should never happen since TokenStringBuffer doesn't throw IOEs. throw new RuntimeException(e); } }
@Override public IFormattedRegion format(final ICompositeNode root, final int offset, final int length) { final TokenStringBuffer buf = new TokenStringBuffer(); final ITokenStream out = offset == 0 ? buf : new FilterStream(buf); final ITokenStream formatStream = formatter.createFormatterStream(null, out, false); if (!(formatStream instanceof IDelegatingTokenStream)) { return super.format(root, offset, length); } try { ITextRegion range; if (offset > 0) { int noffset = root.getText().lastIndexOf('\n', offset) + 1; // Keep the new line int nlength = length + (offset - noffset); // Always start in the beginning of the line range = nodeModelStreamer.feedTokenStream(formatStream, root, noffset, nlength); } else { range = nodeModelStreamer.feedTokenStream(formatStream, root, offset, length); } return new FormattedRegion(range.getOffset(), range.getLength(), buf.toString()); } catch (IOException e) { // this should never happen since TokenStringBuffer doesn't throw IOEs. throw new RuntimeException(e); // NOPMD } }
@Override public String serialize(EObject obj, SaveOptions options) { TokenStringBuffer tokenStringBuffer = new TokenStringBuffer(); try { serialize(obj, tokenStringBuffer, options); } catch (IOException e) { throw new RuntimeException(e); } return tokenStringBuffer.toString(); }
@Override public ReplaceRegion serializeReplacement(EObject obj, SaveOptions options) { TokenStringBuffer tokenStringBuffer = new TokenStringBuffer(); try { TreeConstructionReport report = serialize(obj, tokenStringBuffer, options); return new ReplaceRegion(report.getPreviousLocation(), tokenStringBuffer.toString()); } catch (IOException e) { throw new RuntimeException(e); } }
@Override public String toString() { TokenStringBuffer result = new TokenStringBuffer(); try { flush(result, entries.size()); } catch (IOException e) { e.printStackTrace(); return "Error: " + e.getMessage(); } return result.toString(); }
/** * This method implementation is copied from {@link Serializer#serialize(EObject, SaveOptions)} with the addition of the indentation parameter. {@inheritDoc} */ @Override public String serialize(final EObject obj, final SaveOptions options, final String initialIndentation) { TokenStringBuffer tokenStringBuffer = new TokenStringBuffer(); try { serialize(obj, tokenStringBuffer, options, initialIndentation); } catch (IOException e) { throw new RuntimeException(e);// NOPMD } return tokenStringBuffer.toString(); }