@Override protected String serializeInternal(INode node) { if (type == null) return null; switch (type) { case CROSS_REFERENCE: String ref = crossRefSerializer.serializeCrossRef(eObjectConsumer.getEObject(), (CrossReference) element, (EObject) value, node); if (ref == null) { Assignment ass = GrammarUtil.containingAssignment(element); throw new XtextSerializationException("Could not serialize cross reference from " + EmfFormatter.objPath(eObjectConsumer.getEObject()) + "." + ass.getFeature() + " to " + EmfFormatter.objPath((EObject) value)); } return ref; case KEYWORD: return keywordSerializer.serializeAssignedKeyword(eObjectConsumer.getEObject(), ((Keyword) element), value, node); case TERMINAL_RULE_CALL: return valueSerializer.serializeAssignedValue(eObjectConsumer.getEObject(), (RuleCall) element, value, node); case ENUM_RULE_CALL: return enumLitSerializer.serializeAssignedEnumLiteral(eObjectConsumer.getEObject(), (RuleCall) element, value, node); case PARSER_RULE_CALL: return null; case DATATYPE_RULE_CALL: return valueSerializer.serializeAssignedValue(eObjectConsumer.getEObject(), (RuleCall) element, value, node); default: return null; } }
protected AbstractToken serialize(EObject object, AbstractToken currentToken, TreeConstructionReportImpl rep) { if (object == null) throw new NullPointerException("The to-be-serialized EObject is null"); IEObjectConsumer currentInstance = currentToken.getEObjectConsumer(); int followerIndex = 0, depth = 0; boolean lastSuccess = true; while (currentToken != null) { AbstractToken nextToken = null; IEObjectConsumer nextInstance = null; if ((nextToken = currentToken.createFollower(followerIndex, currentInstance)) != null) { while (nextToken != null && (nextInstance = nextToken.tryConsume()) == null) nextToken = currentToken.createFollower(++followerIndex, currentInstance); } if (nextToken instanceof RootToken && nextToken.getNext() != null) return nextToken.getNext(); if (nextToken != null && nextInstance != null) { if (log.isTraceEnabled()) log.trace(debug(currentToken, currentInstance) + " -> found -> " + currentToken.serialize(null)); currentToken = nextToken; currentInstance = nextInstance; followerIndex = 0; lastSuccess = true; depth++; } else { if (log.isTraceEnabled()) log.trace(debug(currentToken, currentInstance) + " -> fail -> " + (currentToken.getTransitionIndex() + 1)); if (lastSuccess) rep.addDeadEnd(depth, currentToken); followerIndex = currentToken.getTransitionIndex() + 1; currentInstance = currentToken.getEObjectConsumer(); currentToken = currentToken.getNext(); lastSuccess = false; depth--; } } throw new XtextSerializationException(rep, "Serialization failed"); }
/** * Saves the resource corresponding to a program and makes sure that the * result is just the same as the original program source. * * @throws IOException */ public void testSaveResource2() throws IOException { String program = "class A extends Object {}"; Resource resource = loadFromString(program); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { resource.save(outputStream, null); } catch (XtextSerializationException e) { e.printStackTrace(); throw e; } // the saved program must not contain the implicit class object assertEquals(program, outputStream.toString()); }