private static void generateSerializeOnXml ( BindTypeContext context , BindEntity entity ) { // @formatter:off MethodSpec . Builder methodBuilder = MethodSpec . methodBuilder ( "serializeOnXml" ) . addJavadoc ( "method for xml serialization\n" ) . addAnnotation ( Override . class ) . addModifiers ( Modifier . PUBLIC ) // .addParameter(typeName(KriptonXmlContext.class), "context") . addParameter ( typeName ( entity . getElement ( ) ) , "object" ) . addParameter ( typeName ( XMLSerializer . class ) , "xmlSerializer" ) . addParameter ( typeName ( Integer . TYPE ) , "currentEventType" ) . returns ( Void . TYPE ) . addException ( Exception . class ) ; // @formatter:on methodBuilder . beginControlFlow ( "if (currentEventType == 0)" ) ; methodBuilder . addStatement ( "xmlSerializer.writeStartElement(\"$L\")" , entity . xmlInfo . label ) ; // write namespace for ( Pair < String , String > namespace : entity . xmlInfo . namespaces ) { if ( StringUtils . hasText ( namespace . value0 ) ) { methodBuilder . addStatement ( "xmlSerializer.writeAttribute(\"\", $S, $S)" , "xmlns:" + namespace . value0 , namespace . value1 ) ; } else { methodBuilder . addStatement ( "xmlSerializer.writeAttribute(\"\", $S, $S)" , "xmlns" , namespace . value1 ) ; } } methodBuilder . endControlFlow ( ) ; BindTransform bindTransform ; methodBuilder . addCode ( "\n" ) ; // attributes methodBuilder . addCode ( "// Persisted fields:\n\n" ) ; for ( BindProperty item : entity . getCollection ( ) ) { bindTransform = BindTransformer . lookup ( item ) ; methodBuilder . addCode ( "// field $L (mapped with $S)\n" , item . getName ( ) , BindProperty . xmlName ( item ) ) ; if ( item . hasTypeAdapter ( ) ) { methodBuilder . addCode ( "// field trasformation $L $L \n" , item . typeAdapter . dataType , item . typeAdapter . adapterClazz ) ; } bindTransform . generateSerializeOnXml ( context , methodBuilder , "xmlSerializer" , item . getPropertyType ( ) . getTypeName ( ) , "object" , item ) ; methodBuilder . addCode ( "\n" ) ; } methodBuilder . beginControlFlow ( "if (currentEventType == 0)" ) ; methodBuilder . addStatement ( "xmlSerializer.writeEndElement()" ) ; methodBuilder . endControlFlow ( ) ; // methodBuilder.nextControlFlow("catch($T e)", // typeName(Exception.class)); // methodBuilder.addStatement("e.printStackTrace()"); // methodBuilder.addStatement("throw (new $T(e))", // typeName(KriptonRuntimeException.class)); // methodBuilder.endControlFlow(); context . builder . addMethod ( methodBuilder . build ( ) ) ; }
Generate serialize on xml .