Fix code style for code generated by velocity templates (#1146)

This commit is contained in:
Guillaume Nodet 2023-06-06 13:10:30 +02:00 committed by GitHub
parent c79a7a0272
commit 8b652a8b3e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 910 additions and 1403 deletions

View File

@ -51,8 +51,7 @@ import ${packageModelV4}.${class.Name};
#end #end
@Generated @Generated
public class ${className} public class ${className} {
{
private final boolean deepMerge; private final boolean deepMerge;
@ -76,19 +75,16 @@ public class ${className}
* @param hints A set of key-value pairs that customized merger implementations can use to carry domain-specific * @param hints A set of key-value pairs that customized merger implementations can use to carry domain-specific
* information along, may be <code>null</code>. * information along, may be <code>null</code>.
*/ */
public ${root.name} merge( ${root.name} target, ${root.name} source, boolean sourceDominant, Map<?, ?> hints ) public ${root.name} merge(${root.name} target, ${root.name} source, boolean sourceDominant, Map<?, ?> hints) {
{ Objects.requireNonNull(target, "target cannot be null");
Objects.requireNonNull( target, "target cannot be null" ); if (source == null) {
if ( source == null )
{
return target; return target;
} }
Map<Object, Object> context = new HashMap<>(); Map<Object, Object> context = new HashMap<>();
if ( hints != null ) if (hints != null) {
{ context.putAll(hints);
context.putAll( hints );
} }
return merge${root.name}( target, source, sourceDominant, context ); return merge${root.name}(target, source, sourceDominant, context);
} }
#foreach ( $class in $model.allClasses ) #foreach ( $class in $model.allClasses )
@ -98,121 +94,101 @@ public class ${className}
#foreach ( $cl in $ancestors ) #foreach ( $cl in $ancestors )
#set ( $dummy = $allFields.addAll( $cl.getFields($version) ) ) #set ( $dummy = $allFields.addAll( $cl.getFields($version) ) )
#end #end
protected ${class.name} merge${class.name}( ${class.name} target, ${class.name} source, boolean sourceDominant, Map<Object, Object> context ) protected ${class.name} merge${class.name}(${class.name} target, ${class.name} source, boolean sourceDominant, Map<Object, Object> context) {
{ ${class.name}.Builder builder = ${class.name}.newBuilder(target);
${class.name}.Builder builder = ${class.name}.newBuilder( target ); merge${class.name}(builder, target, source, sourceDominant, context);
merge${class.name}( builder, target, source, sourceDominant, context );
return builder.build(); return builder.build();
} }
protected void merge${class.name}( ${class.name}.Builder builder, ${class.name} target, ${class.name} source, boolean sourceDominant, Map<Object, Object> context ) protected void merge${class.name}(${class.name}.Builder builder, ${class.name} target, ${class.name} source, boolean sourceDominant, Map<Object, Object> context) {
{
#if ( $class.superClass ) #if ( $class.superClass )
merge${class.superClass}( builder, target ,source, sourceDominant, context ); merge${class.superClass}(builder, target ,source, sourceDominant, context);
#end #end
#foreach ( $field in $class.getFields($version) ) #foreach ( $field in $class.getFields($version) )
merge${field.modelClass.name}_${Helper.capitalise($field.name)}( builder, target, source, sourceDominant, context ); merge${field.modelClass.name}_${Helper.capitalise($field.name)}(builder, target, source, sourceDominant, context);
#end #end
} }
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#set ( $capField = ${Helper.capitalise($field.name)} ) #set ( $capField = ${Helper.capitalise($field.name)} )
protected void merge${class.name}_${capField}( ${class.name}.Builder builder, ${class.name} target, ${class.name} source, boolean sourceDominant, Map<Object, Object> context ) protected void merge${class.name}_${capField}(${class.name}.Builder builder, ${class.name} target, ${class.name} source, boolean sourceDominant, Map<Object, Object> context)
{ {
#if ( $field.type == "String" ) #if ( $field.type == "String" )
String src = source.get${capField}(); String src = source.get${capField}();
String tgt = target.get${capField}(); String tgt = target.get${capField}();
if ( src != null && ( sourceDominant || tgt == null ) ) if (src != null && (sourceDominant || tgt == null)) {
{ builder.${field.name}(src);
builder.${field.name}( src );
#if ( $locationTracking ) #if ( $locationTracking )
builder.location( "${field.name}", source.getLocation( "${field.name}" ) ); builder.location("${field.name}", source.getLocation("${field.name}"));
#end #end
} }
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
builder.${field.name}( merge( target.get${capField}(), source.get${capField}(), sourceDominant, e -> e ) ); builder.${field.name}(merge(target.get${capField}(), source.get${capField}(), sourceDominant, e -> e));
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> src = source.get${capField}(); Map<String, String> src = source.get${capField}();
if ( !src.isEmpty() ) if (!src.isEmpty()) {
{
Map<String, String> tgt = target.get${capField}(); Map<String, String> tgt = target.get${capField}();
if ( tgt.isEmpty() ) if (tgt.isEmpty()) {
{ builder.${field.name}(src);
builder.${field.name}( src );
#if ( $locationTracking ) #if ( $locationTracking )
builder.location( "${field.name}", source.getLocation( "${field.name}" ) ); builder.location("${field.name}", source.getLocation("${field.name}"));
#end #end
} } else {
else
{
Map<String, String> merged = new HashMap<>(); Map<String, String> merged = new HashMap<>();
merged.putAll( sourceDominant ? target.get${capField}() : source.get${capField}() ); merged.putAll(sourceDominant ? target.get${capField}() : source.get${capField}());
merged.putAll( sourceDominant ? source.get${capField}() : target.get${capField}() ); merged.putAll(sourceDominant ? source.get${capField}() : target.get${capField}());
builder.${field.name}( merged ); builder.${field.name}(merged);
#if ( $locationTracking ) #if ( $locationTracking )
builder.location( "${field.name}", InputLocation.merge( target.getLocation( "${field.name}" ), source.getLocation( "${field.name}" ), sourceDominant ) ); builder.location("${field.name}", InputLocation.merge(target.getLocation("${field.name}"), source.getLocation("${field.name}"), sourceDominant));
#end #end
} }
} }
#elseif ( $field.to && $field.multiplicity == "1" ) #elseif ( $field.to && $field.multiplicity == "1" )
${field.to} src = source.get${capField}(); ${field.to} src = source.get${capField}();
if ( src != null ) if (src != null) {
{
${field.to} tgt = target.get${capField}(); ${field.to} tgt = target.get${capField}();
if ( tgt == null ) if (tgt == null) {
{ tgt = ${field.to}.newInstance(false);
tgt = ${field.to}.newInstance( false );
} }
${field.to} merged = merge${field.to}( tgt, src, sourceDominant, context ); ${field.to} merged = merge${field.to}(tgt, src, sourceDominant, context);
if ( merged == src ) if (merged == src) {
{ builder.${field.name}(merged);
builder.${field.name}( merged );
#if ( $locationTracking ) #if ( $locationTracking )
builder.location( "${field.name}", source.getLocation( "${field.name}" ) ); builder.location("${field.name}", source.getLocation("${field.name}"));
#end #end
} } else if (merged != tgt) {
else if ( merged != tgt ) builder.${field.name}(merged);
{
builder.${field.name}( merged );
#if ( $locationTracking ) #if ( $locationTracking )
builder.location( "${field.name}", InputLocation.merge( target.getLocation( "${field.name}" ), source.getLocation( "${field.name}" ), sourceDominant ) ); builder.location("${field.name}", InputLocation.merge(target.getLocation("${field.name}"), source.getLocation("${field.name}"), sourceDominant));
#end #end
} }
} }
#elseif ( $field.to && $field.multiplicity == "*" ) #elseif ( $field.to && $field.multiplicity == "*" )
if (deepMerge) { if (deepMerge) {
builder.${field.name}( merge( target.get${capField}(), source.get${capField}(), get${field.to}Key(), builder.${field.name}(merge(target.get${capField}(), source.get${capField}(), get${field.to}Key(),
( t, s ) -> merge${field.to}( t, s, sourceDominant, context ) ) ); (t, s) -> merge${field.to}(t, s, sourceDominant, context)));
} else { } else {
builder.${field.name}( merge( target.get${capField}(), source.get${capField}(), sourceDominant, get${field.to}Key() ) ); builder.${field.name}(merge(target.get${capField}(), source.get${capField}(), sourceDominant, get${field.to}Key()));
} }
#elseif ( $field.type == "DOM" ) #elseif ( $field.type == "DOM" )
XmlNode src = source.getConfiguration(); XmlNode src = source.getConfiguration();
if ( src != null ) if (src != null) {
{
XmlNode tgt = target.getConfiguration(); XmlNode tgt = target.getConfiguration();
if ( tgt == null ) if (tgt == null) {
{ builder.configuration(src);
builder.configuration( src ); } else if (sourceDominant) {
} builder.configuration(src.merge(tgt));
else if ( sourceDominant ) } else {
{ builder.configuration(tgt.merge(src));
builder.configuration( src.merge( tgt ) );
}
else
{
builder.configuration( tgt.merge( src ) );
} }
} }
#elseif ( $field.type == "boolean" ) #elseif ($field.type == "boolean")
if ( sourceDominant ) if (sourceDominant) {
{ builder.${field.name}(source.is${capField}());
builder.${field.name}( source.is${capField}() );
} }
#elseif ( $field.type == "int" || $field.type == "java.nio.file.Path" ) #elseif ($field.type == "int" || $field.type == "java.nio.file.Path")
if ( sourceDominant ) if (sourceDominant) {
{ builder.${field.name}(source.get${capField}());
builder.${field.name}( source.get${capField}() );
} }
#else #else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
@ -225,8 +201,7 @@ public class ${className}
#foreach ( $class in $model.allClasses ) #foreach ( $class in $model.allClasses )
#if ( $class.name != "InputSource" && $class.name != "InputLocation" ) #if ( $class.name != "InputSource" && $class.name != "InputLocation" )
protected KeyComputer<${class.name}> get${class.name}Key() protected KeyComputer<${class.name}> get${class.name}Key() {
{
return v -> v; return v -> v;
} }
#end #end
@ -237,37 +212,30 @@ public class ${className}
* @param <T> the data structure type * @param <T> the data structure type
*/ */
@FunctionalInterface @FunctionalInterface
public interface KeyComputer<T> extends Function<T, Object> public interface KeyComputer<T> extends Function<T, Object> {
{
} }
/** /**
* Merge two lists * Merge two lists
*/ */
public static <T> List<T> merge( List<T> tgt, List<T> src, boolean sourceDominant, KeyComputer<T> computer ) public static <T> List<T> merge(List<T> tgt, List<T> src, boolean sourceDominant, KeyComputer<T> computer) {
{ return merge(tgt, src, computer, (t, s) -> sourceDominant ? s : t);
return merge( tgt, src, computer, ( t, s ) -> sourceDominant ? s : t );
} }
public static <T> List<T> merge( List<T> tgt, List<T> src, KeyComputer<T> computer, BinaryOperator<T> remapping ) public static <T> List<T> merge(List<T> tgt, List<T> src, KeyComputer<T> computer, BinaryOperator<T> remapping) {
{ if (src.isEmpty()) {
if ( src.isEmpty() )
{
return tgt; return tgt;
} }
MergingList<T> list; MergingList<T> list;
if ( tgt instanceof MergingList ) if (tgt instanceof MergingList) {
{
list = (MergingList<T>) tgt; list = (MergingList<T>) tgt;
} } else {
else list = new MergingList<>(computer, src.size() + tgt.size());
{ list.mergeAll(tgt, (t, s) -> s);
list = new MergingList<>( computer, src.size() + tgt.size() );
list.mergeAll( tgt, ( t, s ) -> s );
} }
list.mergeAll( src, remapping ); list.mergeAll(src, remapping);
return list; return list;
} }
@ -275,119 +243,90 @@ public class ${className}
* Merging list * Merging list
* @param <V> * @param <V>
*/ */
private static class MergingList<V> extends AbstractList<V> implements java.io.Serializable private static class MergingList<V> extends AbstractList<V> implements java.io.Serializable {
{
private final KeyComputer<V> keyComputer; private final KeyComputer<V> keyComputer;
private Map<Object, V> map; private Map<Object, V> map;
private List<V> list; private List<V> list;
MergingList( KeyComputer<V> keyComputer, int initialCapacity ) MergingList(KeyComputer<V> keyComputer, int initialCapacity) {
{ this.map = new LinkedHashMap<>(initialCapacity);
this.map = new LinkedHashMap<>( initialCapacity );
this.keyComputer = keyComputer; this.keyComputer = keyComputer;
} }
Object writeReplace() throws ObjectStreamException Object writeReplace() throws ObjectStreamException {
{ return new ArrayList<>(this);
return new ArrayList<>( this );
} }
@Override @Override
public Iterator<V> iterator() public Iterator<V> iterator() {
{ if (map != null) {
if ( map != null )
{
return map.values().iterator(); return map.values().iterator();
} } else {
else
{
return list.iterator(); return list.iterator();
} }
} }
void mergeAll( Collection<V> vs, BinaryOperator<V> remapping ) void mergeAll(Collection<V> vs, BinaryOperator<V> remapping) {
{ if (map == null) {
if ( map == null ) map = list.stream().collect(Collectors.toMap(keyComputer,
{ Function.identity(),
map = list.stream().collect( Collectors.toMap( keyComputer, null,
Function.identity(), LinkedHashMap::new));
null,
LinkedHashMap::new ) );
list = null; list = null;
} }
if ( vs instanceof MergingList && ( (MergingList<V>) vs ).map != null ) if (vs instanceof MergingList && ((MergingList<V>) vs).map != null) {
{ for (Map.Entry<Object, V> e : ((MergingList<V>) vs).map.entrySet()) {
for ( Map.Entry<Object, V> e : ( (MergingList<V>) vs ).map.entrySet() )
{
Object key = e.getKey(); Object key = e.getKey();
V v = e.getValue(); V v = e.getValue();
map.merge( key, v, remapping ); map.merge(key, v, remapping);
} }
} } else {
else for (V v : vs) {
{ Object key = keyComputer.apply(v);
for ( V v : vs ) map.merge(key, v, remapping);
{
Object key = keyComputer.apply( v );
map.merge( key, v, remapping );
} }
} }
} }
@Override @Override
public boolean contains( Object o ) public boolean contains(Object o) {
{ if (map != null) {
if ( map != null ) return map.containsValue(o);
{ } else {
return map.containsValue( o ); return list.contains(o);
}
else
{
return list.contains( o );
} }
} }
private List<V> asList() private List<V> asList() {
{ if (list == null) {
if ( list == null ) list = new ArrayList<>(map.values());
{
list = new ArrayList<>( map.values() );
map = null; map = null;
} }
return list; return list;
} }
@Override @Override
public void add( int index, V element ) public void add(int index, V element) {
{ asList().add(index, element);
asList().add( index, element );
} }
@Override @Override
public V remove( int index ) public V remove(int index) {
{ return asList().remove(index);
return asList().remove( index );
} }
@Override @Override
public V get( int index ) public V get(int index) {
{ return asList().get(index);
return asList().get( index );
} }
@Override @Override
public int size() public int size() {
{ if (map != null) {
if ( map != null )
{
return map.size(); return map.size();
} } else {
else
{
return list.size(); return list.size();
} }
} }

View File

@ -106,58 +106,48 @@ public class ${class.name}
#end #end
{ {
public ${class.name}() public ${class.name}() {
{ this(${packageModelV4}.${class.name}.newInstance());
this( ${packageModelV4}.${class.name}.newInstance() );
} }
public ${class.name}( ${packageModelV4}.${class.name} delegate ) public ${class.name}(${packageModelV4}.${class.name} delegate) {
{ this(delegate, null);
this( delegate, null );
} }
public ${class.name}( ${packageModelV4}.${class.name} delegate, BaseObject parent ) public ${class.name}(${packageModelV4}.${class.name} delegate, BaseObject parent) {
{ super(delegate, parent);
super( delegate, parent );
} }
public ${class.name} clone() public ${class.name} clone(){
{ return new ${class.name}(getDelegate());
return new ${class.name}( getDelegate() );
} }
#if ( $class.superClass ) #if ( $class.superClass )
@Override @Override
#end #end
public ${packageModelV4}.${class.name} getDelegate() public ${packageModelV4}.${class.name} getDelegate() {
{ return (${packageModelV4}.${class.name}) super.getDelegate();
return ( ${packageModelV4}.${class.name} ) super.getDelegate();
} }
@Override @Override
public boolean equals( Object o ) public boolean equals(Object o) {
{ if (this == o) {
if ( this == o )
{
return true; return true;
} }
if ( o == null || !( o instanceof ${class.name} ) ) if (o == null || !(o instanceof ${class.name})) {
{
return false; return false;
} }
${class.name} that = ( ${class.name} ) o; ${class.name} that = (${class.name}) o;
return Objects.equals( this.delegate, that.delegate ); return Objects.equals(this.delegate, that.delegate);
} }
@Override @Override
public int hashCode() public int hashCode() {
{
return getDelegate().hashCode(); return getDelegate().hashCode();
} }
#if ( $class == $root ) #if ( $class == $root )
public String getModelEncoding() public String getModelEncoding() {
{
return getDelegate().getModelEncoding(); return getDelegate().getModelEncoding();
} }
@ -173,52 +163,47 @@ public class ${class.name}
#if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" ) #if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" )
@Nonnull @Nonnull
#end #end
public ${type} ${pfx}${cap}() public ${type} ${pfx}${cap}() {
{
#if ( $field.to != "String" && $field.type == "java.util.List" && $field.multiplicity == "*" ) #if ( $field.to != "String" && $field.type == "java.util.List" && $field.multiplicity == "*" )
return new WrapperList<${field.to}, ${packageModelV4}.${field.to}>( return new WrapperList<${field.to}, ${packageModelV4}.${field.to}>(
() -> getDelegate().get${cap}(), l -> update( getDelegate().with${cap}( l ) ), () -> getDelegate().get${cap}(), l -> update(getDelegate().with${cap}(l)),
d -> new ${field.to}( d, this ), ${field.to}::getDelegate ); d -> new ${field.to}(d, this), ${field.to}::getDelegate);
#elseif ( $field.to == "String" && $field.type == "java.util.Properties" && $field.multiplicity == "*" ) #elseif ( $field.to == "String" && $field.type == "java.util.Properties" && $field.multiplicity == "*" )
return new WrapperProperties( () -> getDelegate().get${cap}(), this::set${cap} ); return new WrapperProperties(() -> getDelegate().get${cap}(), this::set${cap});
#elseif ( $field.to == "String" && $field.type == "java.util.List" && $field.multiplicity == "*" ) #elseif ( $field.to == "String" && $field.type == "java.util.List" && $field.multiplicity == "*" )
return new WrapperList<String, ${field.to}>( () -> getDelegate().get${cap}(), this::set${cap}, s -> s, s -> s ); return new WrapperList<String, ${field.to}>(() -> getDelegate().get${cap}(), this::set${cap}, s -> s, s -> s);
#elseif ( $field.to ) #elseif ( $field.to )
return getDelegate().${pfx}${cap}() != null ? new ${field.to}( getDelegate().${pfx}${cap}(), this ) : null; return getDelegate().${pfx}${cap}() != null ? new ${field.to}(getDelegate().${pfx}${cap}(), this) : null;
#elseif ( $field.type == "DOM" ) #elseif ( $field.type == "DOM" )
return getDelegate().${pfx}${cap}() != null ? new Xpp3Dom( getDelegate().${pfx}${cap}(), this::replace ) : null; return getDelegate().${pfx}${cap}() != null ? new Xpp3Dom(getDelegate().${pfx}${cap}(), this::replace) : null;
#else #else
return getDelegate().${pfx}${cap}(); return getDelegate().${pfx}${cap}();
#end #end
} }
public void set${cap}( ${type} ${field.name} ) public void set${cap}(${type} ${field.name}) {
{ #if ($field.type == "DOM")
#if ( $field.type == "DOM" ) if (!Objects.equals(((Xpp3Dom) ${field.name}).getDom(), getDelegate().${pfx}${cap}())) {
if ( ! Objects.equals( ( ( Xpp3Dom ) ${field.name} ).getDom(), getDelegate().${pfx}${cap}() ) ) update(getDelegate().with${cap}(((Xpp3Dom) ${field.name}).getDom()));
{ ((Xpp3Dom) ${field.name}).setChildrenTracking(this::replace);
update( getDelegate().with${cap}( ( ( Xpp3Dom ) ${field.name} ).getDom() ) );
( ( Xpp3Dom ) ${field.name} ).setChildrenTracking( this::replace );
} }
#elseif( $field.type == "java.util.Properties" ) #elseif( $field.type == "java.util.Properties" )
Map<String, String> map = ${field.name}.entrySet().stream() Map<String, String> map = ${field.name}.entrySet().stream()
.collect( Collectors.toMap( e -> e.getKey().toString(), e -> e.getValue().toString() ) ); .collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));
if ( !Objects.equals( map, getDelegate().get${cap}() ) ) if (!Objects.equals(map, getDelegate().get${cap}())) {
{ update(getDelegate().with${cap}(map));
update( getDelegate().with${cap}( map ) );
} }
#else #else
if ( !Objects.equals( ${field.name}, getDelegate().${pfx}${cap}() ) ) if (!Objects.equals(${field.name}, getDelegate().${pfx}${cap}())) {
{
#if ( $field.to != "String" && $field.type == "java.util.List" && $field.multiplicity == "*" ) #if ( $field.to != "String" && $field.type == "java.util.List" && $field.multiplicity == "*" )
update( getDelegate().with${cap}( update(getDelegate().with${cap}(
${field.name}.stream().map( c -> c.getDelegate() ).collect( Collectors.toList() ) ) ); ${field.name}.stream().map(c -> c.getDelegate()).collect(Collectors.toList())));
${field.name}.forEach( e -> e.childrenTracking = this::replace ); ${field.name}.forEach(e -> e.childrenTracking = this::replace);
#elseif ( $field.to && $field.to != "String" ) #elseif ( $field.to && $field.to != "String" )
update( getDelegate().with${cap}( ${field.name}.getDelegate() ) ); update(getDelegate().with${cap}(${field.name}.getDelegate()));
${field.name}.childrenTracking = this::replace; ${field.name}.childrenTracking = this::replace;
#else #else
update( getDelegate().with${cap}( ${field.name} ) ); update(getDelegate().with${cap}(${field.name}));
#end #end
} }
#end #end
@ -227,32 +212,30 @@ public class ${class.name}
#if ( $field.type == "java.util.List" && $field.multiplicity == "*" ) #if ( $field.type == "java.util.List" && $field.multiplicity == "*" )
#set ( $v = $Helper.singular( ${field.name} ) ) #set ( $v = $Helper.singular( ${field.name} ) )
#set ( $scap = $Helper.capitalise( $v ) ) #set ( $scap = $Helper.capitalise( $v ) )
public void add${scap}( ${field.to} ${v} ) public void add${scap}(${field.to} ${v}) {
{
#if ( $field.to == "String" ) #if ( $field.to == "String" )
update( getDelegate().with${cap}( update(getDelegate().with${cap}(
Stream.concat( getDelegate().get${cap}().stream(), Stream.of( ${v} ) ) Stream.concat(getDelegate().get${cap}().stream(), Stream.of(${v}))
.collect( Collectors.toList() ) ) ); .collect(Collectors.toList())));
#else #else
update( getDelegate().with${cap}( update(getDelegate().with${cap}(
Stream.concat( getDelegate().get${cap}().stream(), Stream.of( ${v}.getDelegate() ) ) Stream.concat(getDelegate().get${cap}().stream(), Stream.of(${v}.getDelegate()))
.collect( Collectors.toList() ) ) ); .collect(Collectors.toList())));
${v}.childrenTracking = this::replace; ${v}.childrenTracking = this::replace;
#end #end
} }
public void remove${scap}( ${field.to} ${v} ) public void remove${scap}(${field.to} ${v}) {
{
#if ( $field.to == "String" ) #if ( $field.to == "String" )
update( getDelegate().with${cap}( update(getDelegate().with${cap}(
getDelegate().get${cap}().stream() getDelegate().get${cap}().stream()
.filter( e -> !Objects.equals( e, ${v} ) ) .filter(e -> !Objects.equals(e, ${v}))
.collect( Collectors.toList() ) ) ); .collect(Collectors.toList())));
#else #else
update( getDelegate().with${cap}( update(getDelegate().with${cap}(
getDelegate().get${cap}().stream() getDelegate().get${cap}().stream()
.filter( e -> !Objects.equals( e, ${v} ) ) .filter(e -> !Objects.equals(e, ${v}))
.collect( Collectors.toList() ) ) ); .collect(Collectors.toList())));
${v}.childrenTracking = null; ${v}.childrenTracking = null;
#end #end
} }
@ -260,68 +243,58 @@ public class ${class.name}
#elseif ( $field.type == "java.util.Properties" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.Properties" && $field.multiplicity == "*" )
#set ( $v = $Helper.singular( ${field.name} ) ) #set ( $v = $Helper.singular( ${field.name} ) )
#set ( $scap = $Helper.capitalise( $v ) ) #set ( $scap = $Helper.capitalise( $v ) )
public void add${scap}( String key, String value ) public void add${scap}(String key, String value) {
{ get${cap}().put(key, value);
get${cap}().put( key, value );
} }
#end #end
#end #end
#if ( $locationTracking ) #if ( $locationTracking )
public InputLocation getLocation( Object key ) public InputLocation getLocation(Object key) {
{ ${packageModelV4}.InputLocation loc = getDelegate().getLocation(key);
${packageModelV4}.InputLocation loc = getDelegate().getLocation( key ); return loc != null ? new InputLocation(loc) : null;
return loc != null ? new InputLocation( loc ) : null;
} }
public void setLocation( Object key, InputLocation location ) public void setLocation(Object key, InputLocation location) {
{ update(${packageModelV4}.${class.name}.newBuilder(getDelegate(), true)
update( ${packageModelV4}.${class.name}.newBuilder( getDelegate(), true ) .location(key, location.toApiLocation()).build());
.location( key, location.toApiLocation() ).build() );
} }
#end #end
protected boolean replace( Object oldDelegate, Object newDelegate ) protected boolean replace(Object oldDelegate, Object newDelegate) {
{ if (super.replace(oldDelegate, newDelegate)) {
if ( super.replace( oldDelegate, newDelegate ) )
{
return true; return true;
} }
#foreach ( $field in $class.getFields($version) ) #foreach ( $field in $class.getFields($version) )
#set ( $cap = $Helper.capitalise( $field.name ) ) #set ( $cap = $Helper.capitalise( $field.name ) )
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} ) #set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
#if ( $field.to && $field.multiplicity != "*" ) #if ( $field.to && $field.multiplicity != "*" )
if ( oldDelegate == getDelegate().get${cap}() ) if (oldDelegate == getDelegate().get${cap}()) {
{ update(getDelegate().with${cap}((${packageModelV4}.${field.to}) newDelegate));
update( getDelegate().with${cap}( ( ${packageModelV4}.${field.to} ) newDelegate ) );
return true; return true;
} }
#elseif ( $field.type == "java.util.List" && $field.to != "String" ) #elseif ($field.type == "java.util.List" && $field.to != "String")
if ( getDelegate().get${cap}().contains( oldDelegate ) ) if (getDelegate().get${cap}().contains(oldDelegate)) {
{ List<${packageModelV4}.${field.to}> list = new ArrayList<>(getDelegate().get${cap}());
List<${packageModelV4}.${field.to}> list = new ArrayList<>( getDelegate().get${cap}() ); list.replaceAll(d -> d == oldDelegate ? (${packageModelV4}.${field.to}) newDelegate : d);
list.replaceAll( d -> d == oldDelegate ? ( ${packageModelV4}.${field.to} ) newDelegate : d ); update(getDelegate().with${cap}(list));
update( getDelegate().with${cap}( list ) );
return true; return true;
} }
#elseif ( $field.type == "DOM" ) #elseif ( $field.type == "DOM" )
if ( getDelegate().get${cap}() == oldDelegate ) if (getDelegate().get${cap}() == oldDelegate) {
{ update(getDelegate().with${cap}((org.apache.maven.api.xml.XmlNode) newDelegate));
update( getDelegate().with${cap}( ( org.apache.maven.api.xml.XmlNode ) newDelegate ) );
} }
#end #end
#end #end
return false; return false;
} }
public static List<${packageModelV4}.${class.name}> ${Helper.uncapitalise(${class.name})}ToApiV4( List<${class.name}> list ) public static List<${packageModelV4}.${class.name}> ${Helper.uncapitalise(${class.name})}ToApiV4(List<${class.name}> list) {
{ return list != null ? new WrapperList<>(list, ${class.name}::getDelegate, ${class.name}::new) : null;
return list != null ? new WrapperList<>( list, ${class.name}::getDelegate, ${class.name}::new ) : null;
} }
public static List<${class.name}> ${Helper.uncapitalise(${class.name})}ToApiV3( List<${packageModelV4}.${class.name}> list ) public static List<${class.name}> ${Helper.uncapitalise(${class.name})}ToApiV3(List<${packageModelV4}.${class.name}> list) {
{ return list != null ? new WrapperList<>(list, ${class.name}::new, ${class.name}::getDelegate) : null;
return list != null ? new WrapperList<>( list, ${class.name}::new, ${class.name}::getDelegate ) : null;
} }
#foreach ( $cs in $class.getCodeSegments($version) ) #foreach ( $cs in $class.getCodeSegments($version) )

View File

@ -151,8 +151,7 @@ public class ${class.name}
#if ( $locationTracking ) #if ( $locationTracking )
Map<Object, InputLocation> locations Map<Object, InputLocation> locations
#end #end
) ) {
{
#if ( $class.superClass ) #if ( $class.superClass )
super( super(
#foreach ( $field in $inheritedFields ) #foreach ( $field in $inheritedFields )
@ -169,44 +168,39 @@ public class ${class.name}
#end #end
#foreach ( $field in $class.getFields($version) ) #foreach ( $field in $class.getFields($version) )
#if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" || $field.type == "java.util.Map" ) #if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" || $field.type == "java.util.Map" )
this.${field.name} = ImmutableCollections.copy( ${field.name} ); this.${field.name} = ImmutableCollections.copy(${field.name});
#else #else
this.${field.name} = ${field.name}; this.${field.name} = ${field.name};
#end #end
#end #end
#if ( $locationTracking ) #if ( $locationTracking )
#if ( ! $class.superClass ) #if ( ! $class.superClass )
this.locations = ImmutableCollections.copy( locations ); this.locations = ImmutableCollections.copy(locations);
#end #end
#end #end
} }
#if ( ! $eq.empty ) #if ( ! $eq.empty )
@Override @Override
public boolean equals( Object o ) public boolean equals(Object o) {
{ if (this == o) {
if ( this == o )
{
return true; return true;
} }
if ( o == null || !( o instanceof ${class.name} ) ) if (o == null || !(o instanceof ${class.name})) {
{
return false; return false;
} }
${class.name} that = ( ${class.name} ) o; ${class.name} that = (${class.name}) o;
return ${eq}; return ${eq};
} }
@Override @Override
public int hashCode() public int hashCode() {
{ return Objects.hash(${hc});
return Objects.hash( ${hc} );
} }
#end #end
#if ( $class == $root ) #if ( $class == $root )
public String getModelEncoding() public String getModelEncoding() {
{
return modelEncoding; return modelEncoding;
} }
@ -230,8 +224,7 @@ public class ${class.name}
#if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" ) #if ( $field.type == "java.util.List" || $field.type == "java.util.Properties" )
@Nonnull @Nonnull
#end #end
public ${type} ${pfx}${cap}() public ${type} ${pfx}${cap}() {
{
return this.${field.name}; return this.${field.name};
} }
@ -240,9 +233,8 @@ public class ${class.name}
/** /**
* Gets the location of the specified field in the input source. * Gets the location of the specified field in the input source.
*/ */
public InputLocation getLocation( Object key ) public InputLocation getLocation(Object key) {
{ return locations != null ? locations.get(key) : null;
return locations != null ? locations.get( key ) : null;
} }
#end #end
@ -252,9 +244,8 @@ public class ${class.name}
* @return a {@code Builder} * @return a {@code Builder}
*/ */
@Nonnull @Nonnull
public Builder with() public Builder with() {
{ return newBuilder(this);
return newBuilder( this );
} }
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#set ( $cap = $Helper.capitalise( $field.name ) ) #set ( $cap = $Helper.capitalise( $field.name ) )
@ -269,49 +260,45 @@ public class ${class.name}
* @return a {@code ${class.name}} with the specified ${field.name} * @return a {@code ${class.name}} with the specified ${field.name}
*/ */
@Nonnull @Nonnull
public ${class.name} with${cap}( $type $field.name ) public ${class.name} with${cap}($type $field.name) {
{ return newBuilder(this, true).${field.name}($field.name).build();
return newBuilder(this, true).${field.name}( $field.name ).build();
} }
#end #end
/** /**
* Creates a new {@code ${class.name}} instance. * Creates a new {@code ${class.name}} instance.
* Equivalent to {@code newInstance( true )}. * Equivalent to {@code newInstance(true)}.
* @see #newInstance(boolean) * @see #newInstance(boolean)
* *
* @return a new {@code ${class.name}} * @return a new {@code ${class.name}}
*/ */
@Nonnull @Nonnull
public static ${class.name} newInstance() public static ${class.name} newInstance() {
{ return newInstance(true);
return newInstance( true );
} }
/** /**
* Creates a new {@code ${class.name}} instance using default values or not. * Creates a new {@code ${class.name}} instance using default values or not.
* Equivalent to {@code newBuilder( withDefaults ).build()}. * Equivalent to {@code newBuilder(withDefaults).build()}.
* *
* @param withDefaults the boolean indicating whether default values should be used * @param withDefaults the boolean indicating whether default values should be used
* @return a new {@code ${class.name}} * @return a new {@code ${class.name}}
*/ */
@Nonnull @Nonnull
public static ${class.name} newInstance( boolean withDefaults ) public static ${class.name} newInstance(boolean withDefaults) {
{ return newBuilder(withDefaults).build();
return newBuilder( withDefaults ).build();
} }
/** /**
* Creates a new {@code ${class.name}} builder instance. * Creates a new {@code ${class.name}} builder instance.
* Equivalent to {@code newBuilder( true )}. * Equivalent to {@code newBuilder(true)}.
* @see #newBuilder(boolean) * @see #newBuilder(boolean)
* *
* @return a new {@code Builder} * @return a new {@code Builder}
*/ */
@Nonnull @Nonnull
public static Builder newBuilder() public static Builder newBuilder() {
{ return newBuilder(true);
return newBuilder( true );
} }
/** /**
@ -321,22 +308,20 @@ public class ${class.name}
* @return a new {@code Builder} * @return a new {@code Builder}
*/ */
@Nonnull @Nonnull
public static Builder newBuilder( boolean withDefaults ) public static Builder newBuilder(boolean withDefaults) {
{ return new Builder(withDefaults);
return new Builder( withDefaults );
} }
/** /**
* Creates a new {@code ${class.name}} builder instance using the specified object as a basis. * Creates a new {@code ${class.name}} builder instance using the specified object as a basis.
* Equivalent to {@code newBuilder( from, false )}. * Equivalent to {@code newBuilder(from, false)}.
* *
* @param from the {@code ${class.name}} instance to use as a basis * @param from the {@code ${class.name}} instance to use as a basis
* @return a new {@code Builder} * @return a new {@code Builder}
*/ */
@Nonnull @Nonnull
public static Builder newBuilder( ${class.name} from ) public static Builder newBuilder(${class.name} from) {
{ return newBuilder(from, false);
return newBuilder( from, false );
} }
/** /**
@ -347,9 +332,8 @@ public class ${class.name}
* @return a new {@code Builder} * @return a new {@code Builder}
*/ */
@Nonnull @Nonnull
public static Builder newBuilder( ${class.name} from, boolean forceCopy ) public static Builder newBuilder(${class.name} from, boolean forceCopy) {
{ return new Builder(from, forceCopy);
return new Builder( from, forceCopy );
} }
/** /**
@ -384,13 +368,11 @@ public class ${class.name}
Map<Object, InputLocation> locations; Map<Object, InputLocation> locations;
#end #end
Builder( boolean withDefaults ) Builder(boolean withDefaults) {
{
#if ( $class.superClass ) #if ( $class.superClass )
super( withDefaults ); super(withDefaults);
#end #end
if ( withDefaults ) if (withDefaults) {
{
#foreach ( $field in $class.getFields($version) ) #foreach ( $field in $class.getFields($version) )
#if ( $field.defaultValue ) #if ( $field.defaultValue )
#set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} ) #set ( $type = ${types.getOrDefault($field,${types.getOrDefault($field.type,$field.type)})} )
@ -404,30 +386,25 @@ public class ${class.name}
} }
} }
Builder( ${class.name} base, boolean forceCopy ) Builder(${class.name} base, boolean forceCopy) {
{
#if ( $class.superClass ) #if ( $class.superClass )
super( base, forceCopy ); super(base, forceCopy);
#end #end
if ( forceCopy ) if (forceCopy) {
{
#foreach ( $field in $class.getFields($version) ) #foreach ( $field in $class.getFields($version) )
this.${field.name} = base.${field.name}; this.${field.name} = base.${field.name};
#end #end
#if ( $locationTracking ) #if ( $locationTracking )
this.locations = base.locations; this.locations = base.locations;
#end #end
} } else {
else
{
this.base = base; this.base = base;
} }
} }
#if ( $class == $root ) #if ( $class == $root )
@Nonnull @Nonnull
public Builder modelEncoding( String modelEncoding ) public Builder modelEncoding(String modelEncoding) {
{
this.modelEncoding = modelEncoding; this.modelEncoding = modelEncoding;
return this; return this;
} }
@ -439,8 +416,7 @@ public class ${class.name}
#set ( $type = ${type.replace('List<','Collection<')} ) #set ( $type = ${type.replace('List<','Collection<')} )
#end #end
@Nonnull @Nonnull
public Builder ${field.name}( ${type} ${field.name} ) public Builder ${field.name}(${type} ${field.name}) {
{
this.${field.name} = ${field.name}; this.${field.name} = ${field.name};
return this; return this;
} }
@ -449,29 +425,24 @@ public class ${class.name}
#if ( $locationTracking ) #if ( $locationTracking )
@Nonnull @Nonnull
public Builder location( Object key, InputLocation location ) public Builder location(Object key, InputLocation location) {
{ if (location != null) {
if ( location != null ) if (!(this.locations instanceof HashMap)) {
{ this.locations = this.locations != null ? new HashMap<>(this.locations) : new HashMap<>();
if ( !(this.locations instanceof HashMap) )
{
this.locations = this.locations != null ? new HashMap<>( this.locations ) : new HashMap<>();
} }
this.locations.put( key, location ); this.locations.put(key, location);
} }
return this; return this;
} }
#end #end
@Nonnull @Nonnull
public ${class.name} build() public ${class.name} build() {
{ if (base != null
if ( base != null
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
&& ( ${field.name} == null || ${field.name} == base.${field.name} ) && (${field.name} == null || ${field.name} == base.${field.name})
#end #end
) ) {
{
return base; return base;
} }
#if ( $locationTracking ) #if ( $locationTracking )
@ -480,25 +451,24 @@ public class ${class.name}
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
InputLocation ${field.name}Location = null; InputLocation ${field.name}Location = null;
#end #end
if ( this.locations != null ) if (this.locations != null) {
{
locations = this.locations; locations = this.locations;
} }
#end #end
return new ${class.name}( return new ${class.name}(
#if ( $class == $root ) #if ( $class == $root )
modelEncoding != null ? modelEncoding : ( base != null ? base.modelEncoding : "UTF-8" ), modelEncoding != null ? modelEncoding : (base != null ? base.modelEncoding : "UTF-8"),
#end #end
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#set ( $sep = "#if(${locationTracking}||$field!=${allFields[${allFields.size()} - 1]}),#end" ) #set ( $sep = "#if(${locationTracking}||$field!=${allFields[${allFields.size()} - 1]}),#end" )
#if ( $field.type == "boolean" || $field.type == "int" ) #if ( $field.type == "boolean" || $field.type == "int" )
${field.name} != null ? ${field.name} : ( base != null ? base.${field.name} : ${field.defaultValue} )${sep} ${field.name} != null ? ${field.name} : (base != null ? base.${field.name} : ${field.defaultValue})${sep}
#else #else
${field.name} != null ? ${field.name} : ( base != null ? base.${field.name} : null )${sep} ${field.name} != null ? ${field.name} : (base != null ? base.${field.name} : null)${sep}
#end #end
#end #end
#if ( $locationTracking ) #if ( $locationTracking )
locations != null ? locations : ( base != null ? base.locations : null ) locations != null ? locations : (base != null ? base.locations : null)
#end #end
); );
} }

View File

@ -60,19 +60,17 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParser;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@Generated @Generated
public class ${className} public class ${className} {
{
private boolean addDefaultEntities = true; private boolean addDefaultEntities = true;
private final ContentTransformer contentTransformer; private final ContentTransformer contentTransformer;
public ${className}() public ${className}() {
{ this((s, f) -> s);
this( ( s, f ) -> s );
} }
public ${className}( ContentTransformer contentTransformer ) public ${className}(ContentTransformer contentTransformer) {
{
this.contentTransformer = contentTransformer; this.contentTransformer = contentTransformer;
} }
@ -81,8 +79,7 @@ public class ${className}
* *
* @return boolean * @return boolean
*/ */
public boolean getAddDefaultEntities() public boolean getAddDefaultEntities() {
{
return addDefaultEntities; return addDefaultEntities;
} //-- boolean getAddDefaultEntities() } //-- boolean getAddDefaultEntities()
@ -91,10 +88,9 @@ public class ${className}
* *
* @param addDefaultEntities a addDefaultEntities object. * @param addDefaultEntities a addDefaultEntities object.
*/ */
public void setAddDefaultEntities( boolean addDefaultEntities ) public void setAddDefaultEntities(boolean addDefaultEntities) {
{
this.addDefaultEntities = addDefaultEntities; this.addDefaultEntities = addDefaultEntities;
} //-- void setAddDefaultEntities( boolean ) } //-- void setAddDefaultEntities(boolean)
/** /**
* @see ReaderFactory#newXmlReader * @see ReaderFactory#newXmlReader
@ -106,13 +102,11 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( Reader reader, boolean strict, InputSource source ) public ${root.name} read(Reader reader, boolean strict, InputSource source) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser();
{ parser.setInput(reader);
XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser( ); return read(parser, strict, source);
parser.setInput( reader ); } //-- ${root.name} read(Reader, boolean)
return read( parser, strict, source );
} //-- ${root.name} read( Reader, boolean )
/** /**
* Method read. * Method read.
@ -124,11 +118,9 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( InputStream in, boolean strict, InputSource source ) public ${root.name} read(InputStream in, boolean strict, InputSource source) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException return read(ReaderFactory.newXmlReader(in), strict, source);
{ } //-- ${root.name} read(InputStream, boolean)
return read( ReaderFactory.newXmlReader( in ), strict, source );
} //-- ${root.name} read( InputStream, boolean )
/** /**
* Method read. * Method read.
@ -140,36 +132,28 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( XmlPullParser parser, boolean strict, InputSource source ) public ${root.name} read(XmlPullParser parser, boolean strict, InputSource source) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException
{
$rootUcapName $rootLcapName = null; $rootUcapName $rootLcapName = null;
int eventType = parser.getEventType(); int eventType = parser.getEventType();
boolean parsed = false; boolean parsed = false;
while ( eventType != XmlPullParser.END_DOCUMENT ) while (eventType != XmlPullParser.END_DOCUMENT) {
{ if (eventType == XmlPullParser.START_TAG) {
if ( eventType == XmlPullParser.START_TAG ) if (strict && ! "${rootTag}".equals(parser.getName())) {
{ throw new XmlPullParserException("Expected root element '${rootTag}' but found '" + parser.getName() + "'", parser, null);
if ( strict && ! "${rootTag}".equals( parser.getName() ) ) } else if (parsed) {
{
throw new XmlPullParserException( "Expected root element '${rootTag}' but found '" + parser.getName() + "'", parser, null );
}
else if ( parsed )
{
// fallback, already expected a XmlPullParserException due to invalid XML // fallback, already expected a XmlPullParserException due to invalid XML
throw new XmlPullParserException( "Duplicated tag: '${rootTag}'", parser, null ); throw new XmlPullParserException("Duplicated tag: '${rootTag}'", parser, null);
} }
$rootLcapName = parse${rootUcapName}( parser, strict, source ); $rootLcapName = parse${rootUcapName}(parser, strict, source);
parsed = true; parsed = true;
} }
eventType = parser.next(); eventType = parser.next();
} }
if ( parsed ) if (parsed) {
{
return $rootLcapName; return $rootLcapName;
} }
throw new XmlPullParserException( "Expected root element '${rootTag}' but found no element at all: invalid XML document", parser, null ); throw new XmlPullParserException("Expected root element '${rootTag}' but found no element at all: invalid XML document", parser, null);
} //-- ${root.name} read( XmlPullParser, boolean ) } //-- ${root.name} read(XmlPullParser, boolean)
#foreach ( $class in $model.allClasses ) #foreach ( $class in $model.allClasses )
#if ( $class.name != "InputSource" && $class.name != "InputLocation" ) #if ( $class.name != "InputSource" && $class.name != "InputLocation" )
@ -177,61 +161,48 @@ public class ${className}
#set ( $classLcapName = $Helper.uncapitalise( $class.name ) ) #set ( $classLcapName = $Helper.uncapitalise( $class.name ) )
#set ( $ancestors = $Helper.ancestors( $class ) ) #set ( $ancestors = $Helper.ancestors( $class ) )
#set ( $allFields = $Helper.xmlFields( $class ) ) #set ( $allFields = $Helper.xmlFields( $class ) )
private ${classUcapName} parse${classUcapName}( XmlPullParser parser, boolean strict, InputSource source ) private ${classUcapName} parse${classUcapName}(XmlPullParser parser, boolean strict, InputSource source) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException
{
String tagName = parser.getName(); String tagName = parser.getName();
${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder( true ); ${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder(true);
${classLcapName}.location( "", new InputLocation( parser.getLineNumber(), parser.getColumnNumber(), source ) ); ${classLcapName}.location("", new InputLocation(parser.getLineNumber(), parser.getColumnNumber(), source));
for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- ) for (int i = parser.getAttributeCount() - 1; i >= 0; i--) {
{ String name = parser.getAttributeName(i);
String name = parser.getAttributeName( i ); String value = parser.getAttributeValue(i);
String value = parser.getAttributeValue( i ); if (name.indexOf(':') >= 0) {
if ( name.indexOf( ':' ) >= 0 )
{
// just ignore attributes with non-default namespace (for example: xmlns:xsi) // just ignore attributes with non-default namespace (for example: xmlns:xsi)
}
#if ( $class == $root ) #if ( $class == $root )
else if ( "xmlns".equals( name ) ) } else if ("xmlns".equals(name)) {
{
// ignore xmlns attribute in root class, which is a reserved attribute name // ignore xmlns attribute in root class, which is a reserved attribute name
}
#end #end
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#if ( $Helper.xmlFieldMetadata( $field ).attribute ) #if ( $Helper.xmlFieldMetadata( $field ).attribute )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName ) #set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#set ( $fieldCapName = $Helper.capitalise( $field.name ) ) #set ( $fieldCapName = $Helper.capitalise( $field.name ) )
else if ( "$fieldTagName".equals( name ) ) } else if ("$fieldTagName".equals(name)) {
{ ${classLcapName}.location(name, new InputLocation(parser.getLineNumber(), parser.getColumnNumber(), source));
${classLcapName}.location( name, new InputLocation( parser.getLineNumber(), parser.getColumnNumber(), source ) );
#if ( $field.type == "String" ) #if ( $field.type == "String" )
${classLcapName}.${field.name}( interpolatedTrimmed( value, "$fieldTagName" ) ); ${classLcapName}.${field.name}(interpolatedTrimmed(value, "$fieldTagName"));
#elseif ( $field.type == "boolean" || $field.type == "Boolean" ) #elseif ( $field.type == "boolean" || $field.type == "Boolean" )
${classLcapName}.${field.name}( getBooleanValue( interpolatedTrimmed( value, "$fieldTagName" ), "$fieldTagName", parser, ${field.defaultValue} ) ); ${classLcapName}.${field.name}(getBooleanValue(interpolatedTrimmed(value, "$fieldTagName"), "$fieldTagName", parser, ${field.defaultValue}));
#else #else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
#end #end
}
#end #end
#end #end
else } else {
{ checkUnknownAttribute(parser, name, tagName, strict);
checkUnknownAttribute( parser, name, tagName, strict );
} }
} }
Set<String> parsed = new HashSet<>(); Set<String> parsed = new HashSet<>();
while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG ) while ((strict ? parser.nextTag() : nextTag(parser)) == XmlPullParser.START_TAG) {
{ String childName = unalias(parser.getName());
String childName = unalias( parser.getName() ); if (!parsed.add(childName)) {
if ( !parsed.add( childName ) ) throw new XmlPullParserException("Duplicated tag: '" + childName + "'", parser, null);
{
throw new XmlPullParserException( "Duplicated tag: '" + childName + "'", parser, null );
} }
int line = parser.getLineNumber(); int line = parser.getLineNumber();
int column = parser.getColumnNumber(); int column = parser.getColumnNumber();
Map<Object, InputLocation> locations = null; Map<Object, InputLocation> locations = null;
switch ( childName ) switch (childName) {
{
#set( $ift = "if" ) #set( $ift = "if" )
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#if ( ! $Helper.xmlFieldMetadata( $field ).attribute && ! $Helper.xmlFieldMetadata( $field ).transient ) #if ( ! $Helper.xmlFieldMetadata( $field ).attribute && ! $Helper.xmlFieldMetadata( $field ).transient )
@ -239,67 +210,57 @@ public class ${className}
#if ( ! $fieldTagName ) #if ( ! $fieldTagName )
#set ( $fieldTagName = $field.name ) #set ( $fieldTagName = $field.name )
#end #end
#set ( $fieldCapName = $Helper.capitalise( $field.name ) ) #set ( $fieldCapName = $Helper.capitalise($field.name))
case "${fieldTagName}": case "${fieldTagName}": {
{
#if ( $field.type == "String" ) #if ( $field.type == "String" )
${classLcapName}.${field.name}( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ) ); ${classLcapName}.${field.name}(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"));
break; break;
#elseif ( $field.type == "boolean" || $field.type == "Boolean" ) #elseif ( $field.type == "boolean" || $field.type == "Boolean" )
${classLcapName}.${field.name}( getBooleanValue( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ), "${fieldTagName}", parser, ${field.defaultValue} ) ); ${classLcapName}.${field.name}(getBooleanValue(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"), "${fieldTagName}", parser, ${field.defaultValue}));
break; break;
#elseif ( $field.type == "int" ) #elseif ( $field.type == "int" )
${classLcapName}.${field.name}( getIntegerValue( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ), "${fieldTagName}", parser, strict, ${field.defaultValue} ) ); ${classLcapName}.${field.name}(getIntegerValue(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"), "${fieldTagName}", parser, strict, ${field.defaultValue}));
break; break;
#elseif ( $field.type == "DOM" ) #elseif ( $field.type == "DOM" )
${classLcapName}.${field.name}( XmlNodeBuilder.build( parser, true ) ); ${classLcapName}.${field.name}(XmlNodeBuilder.build(parser, true));
break; break;
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
List<String> ${field.name} = new ArrayList<>(); List<String> ${field.name} = new ArrayList<>();
locations = new HashMap<>(); locations = new HashMap<>();
while ( parser.nextTag() == XmlPullParser.START_TAG ) while (parser.nextTag() == XmlPullParser.START_TAG) {
{ if ("${Helper.singular($fieldTagName)}".equals(parser.getName())) {
if ( "${Helper.singular($fieldTagName)}".equals( parser.getName() ) ) locations.put(Integer.valueOf(locations.size()), new InputLocation(parser.getLineNumber(), parser.getColumnNumber(), source));
{ ${field.name}.add(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"));
locations.put( Integer.valueOf( locations.size() ), new InputLocation( parser.getLineNumber(), parser.getColumnNumber(), source ) ); } else {
${field.name}.add( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ) ); checkUnknownElement(parser, strict);
}
else
{
checkUnknownElement( parser, strict );
} }
} }
${classLcapName}.${field.name}( ${field.name} ); ${classLcapName}.${field.name}(${field.name});
break; break;
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> ${field.name} = new LinkedHashMap<>(); Map<String, String> ${field.name} = new LinkedHashMap<>();
locations = new HashMap<>(); locations = new HashMap<>();
while ( parser.nextTag() == XmlPullParser.START_TAG ) while (parser.nextTag() == XmlPullParser.START_TAG) {
{
String key = parser.getName(); String key = parser.getName();
String value = parser.nextText().trim(); String value = parser.nextText().trim();
locations.put( key, new InputLocation( parser.getLineNumber(), parser.getColumnNumber(), source ) ); locations.put(key, new InputLocation(parser.getLineNumber(), parser.getColumnNumber(), source));
${field.name}.put( key, value ); ${field.name}.put(key, value);
} }
${classLcapName}.${field.name}( ${field.name} ); ${classLcapName}.${field.name}(${field.name});
break; break;
#elseif ( $field.to && $field.multiplicity == "1" ) #elseif ( $field.to && $field.multiplicity == "1" )
${classLcapName}.${field.name}( parse${field.toClass.name}( parser, strict, source ) ); ${classLcapName}.${field.name}(parse${field.toClass.name}(parser, strict, source ));
break; break;
#elseif ( $field.to && $field.multiplicity == "*" ) #elseif ( $field.to && $field.multiplicity == "*" )
List<$field.to> ${field.name} = new ArrayList<>(); List<$field.to> ${field.name} = new ArrayList<>();
while ( parser.nextTag() == XmlPullParser.START_TAG ) while (parser.nextTag() == XmlPullParser.START_TAG) {
{ if ("${Helper.singular($fieldTagName)}".equals(parser.getName())) {
if ( "${Helper.singular($fieldTagName)}".equals( parser.getName() ) ) ${field.name}.add(parse${field.toClass.name}(parser, strict, source));
{ } else {
${field.name}.add( parse${field.toClass.name}( parser, strict, source ) ); checkUnknownElement(parser, strict);
}
else
{
checkUnknownElement( parser, strict );
} }
} }
${classLcapName}.${field.name}( ${field.name} ); ${classLcapName}.${field.name}(${field.name});
break; break;
#else #else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
@ -309,16 +270,15 @@ public class ${className}
#set( $ift = "else if" ) #set( $ift = "else if" )
#end #end
#end #end
default: default: {
{ checkUnknownElement(parser, strict);
checkUnknownElement( parser, strict );
break; break;
} }
} }
${classLcapName}.location( childName, new InputLocation( line, column, source, locations ) ); ${classLcapName}.location(childName, new InputLocation(line, column, source, locations));
} }
#if ( $class == $root ) #if ( $class == $root )
${classLcapName}.modelEncoding( parser.getInputEncoding() ); ${classLcapName}.modelEncoding(parser.getInputEncoding());
#end #end
return ${classLcapName}.build(); return ${classLcapName}.build();
} }
@ -326,10 +286,8 @@ public class ${className}
#end #end
#end #end
private String unalias( String tagName ) private String unalias(String tagName) {
{ switch (tagName) {
switch ( tagName )
{
#set( $aliases = { } ) #set( $aliases = { } )
#foreach( $class in $model.allClasses ) #foreach( $class in $model.allClasses )
#foreach ( $field in $class.allFields ) #foreach ( $field in $class.allFields )
@ -358,15 +316,12 @@ public class ${className}
* any. * any.
* @throws IOException IOException if any. * @throws IOException IOException if any.
*/ */
private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict ) private void checkUnknownAttribute(XmlPullParser parser, String attribute, String tagName, boolean strict) throws XmlPullParserException, IOException {
throws XmlPullParserException, IOException
{
// strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too // strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
if ( strict ) if (strict) {
{ throw new XmlPullParserException("Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null);
throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
} }
} //-- void checkUnknownAttribute( XmlPullParser, String, String, boolean ) } //-- void checkUnknownAttribute(XmlPullParser, String, String, boolean)
/** /**
* Method checkUnknownElement. * Method checkUnknownElement.
@ -377,27 +332,20 @@ public class ${className}
* any. * any.
* @throws IOException IOException if any. * @throws IOException IOException if any.
*/ */
private void checkUnknownElement( XmlPullParser parser, boolean strict ) private void checkUnknownElement(XmlPullParser parser, boolean strict) throws XmlPullParserException, IOException {
throws XmlPullParserException, IOException if (strict) {
{ throw new XmlPullParserException("Unrecognised tag: '" + parser.getName() + "'", parser, null);
if ( strict )
{
throw new XmlPullParserException( "Unrecognised tag: '" + parser.getName() + "'", parser, null );
} }
for ( int unrecognizedTagCount = 1; unrecognizedTagCount > 0; ) for (int unrecognizedTagCount = 1; unrecognizedTagCount > 0;) {
{
int eventType = parser.next(); int eventType = parser.next();
if ( eventType == XmlPullParser.START_TAG ) if (eventType == XmlPullParser.START_TAG) {
{
unrecognizedTagCount++; unrecognizedTagCount++;
} } else if (eventType == XmlPullParser.END_TAG) {
else if ( eventType == XmlPullParser.END_TAG )
{
unrecognizedTagCount--; unrecognizedTagCount--;
} }
} }
} //-- void checkUnknownElement( XmlPullParser, boolean ) } //-- void checkUnknownElement(XmlPullParser, boolean)
/** /**
* Method getTrimmedValue. * Method getTrimmedValue.
@ -405,14 +353,12 @@ public class ${className}
* @param s a s object. * @param s a s object.
* @return String * @return String
*/ */
private String getTrimmedValue( String s ) private String getTrimmedValue(String s) {
{ if (s != null) {
if ( s != null )
{
s = s.trim(); s = s.trim();
} }
return s; return s;
} //-- String getTrimmedValue( String ) } //-- String getTrimmedValue(String)
/** /**
* Method interpolatedTrimmed. * Method interpolatedTrimmed.
@ -421,10 +367,9 @@ public class ${className}
* @param context a context object. * @param context a context object.
* @return String * @return String
*/ */
private String interpolatedTrimmed( String value, String context ) private String interpolatedTrimmed(String value, String context) {
{ return getTrimmedValue(contentTransformer.transform(value, context));
return getTrimmedValue( contentTransformer.transform( value, context ) ); } //-- String interpolatedTrimmed(String, String)
} //-- String interpolatedTrimmed( String, String )
/** /**
* Method nextTag. * Method nextTag.
@ -435,20 +380,16 @@ public class ${className}
* any. * any.
* @return int * @return int
*/ */
private int nextTag( XmlPullParser parser ) private int nextTag(XmlPullParser parser) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException
{
int eventType = parser.next(); int eventType = parser.next();
if ( eventType == XmlPullParser.TEXT ) if (eventType == XmlPullParser.TEXT) {
{
eventType = parser.next(); eventType = parser.next();
} }
if ( eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG ) if (eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG) {
{ throw new XmlPullParserException("expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null);
throw new XmlPullParserException( "expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null );
} }
return eventType; return eventType;
} //-- int nextTag( XmlPullParser ) } //-- int nextTag(XmlPullParser)
#foreach ( $class in $model.allClasses ) #foreach ( $class in $model.allClasses )
#foreach ( $field in $class.getFields($version) ) #foreach ( $field in $class.getFields($version) )
@ -471,15 +412,12 @@ public class ${className}
* any. * any.
* @return boolean * @return boolean
*/ */
private boolean getBooleanValue( String s, String attribute, XmlPullParser parser, boolean defaultValue ) private boolean getBooleanValue(String s, String attribute, XmlPullParser parser, boolean defaultValue) throws XmlPullParserException {
throws XmlPullParserException if (s != null && s.length() != 0) {
{ return Boolean.valueOf(s).booleanValue();
if ( s != null && s.length() != 0 )
{
return Boolean.valueOf( s ).booleanValue();
} }
return defaultValue; return defaultValue;
} //-- boolean getBooleanValue( String, String, XmlPullParser, String ) } //-- boolean getBooleanValue(String, String, XmlPullParser, String)
#end #end
#if ( $hasIntegerField ) #if ( $hasIntegerField )
@ -494,29 +432,21 @@ public class ${className}
* any. * any.
* @return int * @return int
*/ */
private int getIntegerValue( String s, String attribute, XmlPullParser parser, boolean strict, int defaultValue ) private int getIntegerValue(String s, String attribute, XmlPullParser parser, boolean strict, int defaultValue) throws XmlPullParserException {
throws XmlPullParserException if (s != null) {
{ try {
if ( s != null ) return Integer.valueOf(s).intValue();
{ } catch (NumberFormatException nfe) {
try if (strict) {
{ throw new XmlPullParserException("Unable to parse element '" + attribute + "', must be an integer", parser, nfe);
return Integer.valueOf( s ).intValue();
}
catch ( NumberFormatException nfe )
{
if ( strict )
{
throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be an integer", parser, nfe );
} }
} }
} }
return defaultValue; return defaultValue;
} //-- int getIntegerValue( String, String, XmlPullParser, boolean ) } //-- int getIntegerValue(String, String, XmlPullParser, boolean)
#end #end
public static interface ContentTransformer public static interface ContentTransformer {
{
/** /**
* Interpolate the value read from the xpp3 document * Interpolate the value read from the xpp3 document
* @param source The source value * @param source The source value
@ -524,7 +454,7 @@ public class ${className}
* log stuff. * log stuff.
* @return The interpolated value. * @return The interpolated value.
*/ */
String transform( String source, String fieldName ); String transform(String source, String fieldName);
} }
} }

View File

@ -58,19 +58,17 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParser;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@Generated @Generated
public class ${className} public class ${className} {
{
private boolean addDefaultEntities = true; private boolean addDefaultEntities = true;
private final ContentTransformer contentTransformer; private final ContentTransformer contentTransformer;
public ${className}() public ${className}() {
{ this((s, f) -> s);
this( ( s, f ) -> s );
} }
public ${className}( ContentTransformer contentTransformer ) public ${className}(ContentTransformer contentTransformer) {
{
this.contentTransformer = contentTransformer; this.contentTransformer = contentTransformer;
} }
@ -79,8 +77,7 @@ public class ${className}
* *
* @return boolean * @return boolean
*/ */
public boolean getAddDefaultEntities() public boolean getAddDefaultEntities() {
{
return addDefaultEntities; return addDefaultEntities;
} //-- boolean getAddDefaultEntities() } //-- boolean getAddDefaultEntities()
@ -89,10 +86,9 @@ public class ${className}
* *
* @param addDefaultEntities a addDefaultEntities object. * @param addDefaultEntities a addDefaultEntities object.
*/ */
public void setAddDefaultEntities( boolean addDefaultEntities ) public void setAddDefaultEntities(boolean addDefaultEntities) {
{
this.addDefaultEntities = addDefaultEntities; this.addDefaultEntities = addDefaultEntities;
} //-- void setAddDefaultEntities( boolean ) } //-- void setAddDefaultEntities(boolean)
/** /**
* @see ReaderFactory#newXmlReader * @see ReaderFactory#newXmlReader
@ -104,16 +100,11 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( Reader reader, boolean strict ) public ${root.name} read(Reader reader, boolean strict) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser();
{ parser.setInput(reader);
XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser( ); return read(parser, strict);
} //-- ${root.name} read(Reader, boolean)
parser.setInput( reader );
return read( parser, strict );
} //-- ${root.name} read( Reader, boolean )
/** /**
* @see ReaderFactory#newXmlReader * @see ReaderFactory#newXmlReader
@ -124,11 +115,9 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( Reader reader ) public ${root.name} read(Reader reader) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException return read(reader, true);
{ } //-- ${root.name} read(Reader)
return read( reader, true );
} //-- ${root.name} read( Reader )
/** /**
* Method read. * Method read.
@ -140,11 +129,9 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( InputStream in, boolean strict ) public ${root.name} read(InputStream in, boolean strict) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException return read(ReaderFactory.newXmlReader(in), strict);
{ } //-- ${root.name} read(InputStream, boolean)
return read( ReaderFactory.newXmlReader( in ), strict );
} //-- ${root.name} read( InputStream, boolean )
/** /**
* Method read. * Method read.
@ -155,11 +142,9 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( InputStream in ) public ${root.name} read(InputStream in) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException return read(ReaderFactory.newXmlReader(in));
{ } //-- ${root.name} read(InputStream)
return read( ReaderFactory.newXmlReader( in ) );
} //-- ${root.name} read( InputStream )
/** /**
* Method read. * Method read.
@ -171,36 +156,29 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( XmlPullParser parser, boolean strict ) public ${root.name} read(XmlPullParser parser, boolean strict) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException
{
$rootUcapName $rootLcapName = null; $rootUcapName $rootLcapName = null;
int eventType = parser.getEventType(); int eventType = parser.getEventType();
boolean parsed = false; boolean parsed = false;
while ( eventType != XmlPullParser.END_DOCUMENT ) while (eventType != XmlPullParser.END_DOCUMENT) {
{ if (eventType == XmlPullParser.START_TAG) {
if ( eventType == XmlPullParser.START_TAG ) if (strict && ! "${rootTag}".equals(parser.getName())) {
{ throw new XmlPullParserException("Expected root element '${rootTag}' but found '" + parser.getName() + "'", parser, null);
if ( strict && ! "${rootTag}".equals( parser.getName() ) )
{
throw new XmlPullParserException( "Expected root element '${rootTag}' but found '" + parser.getName() + "'", parser, null );
} }
else if ( parsed ) else if (parsed) {
{
// fallback, already expected a XmlPullParserException due to invalid XML // fallback, already expected a XmlPullParserException due to invalid XML
throw new XmlPullParserException( "Duplicated tag: '${rootTag}'", parser, null ); throw new XmlPullParserException("Duplicated tag: '${rootTag}'", parser, null);
} }
$rootLcapName = parse${rootUcapName}( parser, strict ); $rootLcapName = parse${rootUcapName}(parser, strict);
parsed = true; parsed = true;
} }
eventType = parser.next(); eventType = parser.next();
} }
if ( parsed ) if (parsed) {
{
return $rootLcapName; return $rootLcapName;
} }
throw new XmlPullParserException( "Expected root element '${rootTag}' but found no element at all: invalid XML document", parser, null ); throw new XmlPullParserException("Expected root element '${rootTag}' but found no element at all: invalid XML document", parser, null);
} //-- ${root.name} read( XmlPullParser, boolean ) } //-- ${root.name} read(XmlPullParser, boolean)
#foreach ( $class in $model.allClasses ) #foreach ( $class in $model.allClasses )
#if ( $class.name != "InputSource" && $class.name != "InputLocation" ) #if ( $class.name != "InputSource" && $class.name != "InputLocation" )
@ -208,22 +186,17 @@ public class ${className}
#set ( $classLcapName = $Helper.uncapitalise( $class.name ) ) #set ( $classLcapName = $Helper.uncapitalise( $class.name ) )
#set ( $ancestors = $Helper.ancestors( $class ) ) #set ( $ancestors = $Helper.ancestors( $class ) )
#set ( $allFields = $Helper.xmlFields( $class ) ) #set ( $allFields = $Helper.xmlFields( $class ) )
private ${classUcapName} parse${classUcapName}( XmlPullParser parser, boolean strict ) private ${classUcapName} parse${classUcapName}(XmlPullParser parser, boolean strict) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException
{
String tagName = parser.getName(); String tagName = parser.getName();
${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder( true ); ${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder(true);
for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- ) for (int i = parser.getAttributeCount() - 1; i >= 0; i--) {
{ String name = parser.getAttributeName(i);
String name = parser.getAttributeName( i ); String value = parser.getAttributeValue(i);
String value = parser.getAttributeValue( i ); if (name.indexOf(':') >= 0) {
if ( name.indexOf( ':' ) >= 0 )
{
// just ignore attributes with non-default namespace (for example: xmlns:xsi) // just ignore attributes with non-default namespace (for example: xmlns:xsi)
} }
#if ( $class == $root ) #if ( $class == $root )
else if ( "xmlns".equals( name ) ) else if ("xmlns".equals(name)) {
{
// ignore xmlns attribute in root class, which is a reserved attribute name // ignore xmlns attribute in root class, which is a reserved attribute name
} }
#end #end
@ -231,33 +204,28 @@ public class ${className}
#if ( $Helper.xmlFieldMetadata( $field ).attribute ) #if ( $Helper.xmlFieldMetadata( $field ).attribute )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName ) #set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#set ( $fieldCapName = $Helper.capitalise( $field.name ) ) #set ( $fieldCapName = $Helper.capitalise( $field.name ) )
else if ( "$fieldTagName".equals( name ) ) else if ("$fieldTagName".equals(name)) {
{
#if ( $field.type == "String" ) #if ( $field.type == "String" )
${classLcapName}.${field.name}( interpolatedTrimmed( value, "$fieldTagName" ) ); ${classLcapName}.${field.name}(interpolatedTrimmed(value, "$fieldTagName"));
#elseif ( $field.type == "boolean" || $field.type == "Boolean" ) #elseif ( $field.type == "boolean" || $field.type == "Boolean" )
${classLcapName}.${field.name}( getBooleanValue( interpolatedTrimmed( value, "$fieldTagName" ), "$fieldTagName", parser, ${field.defaultValue} ) ); ${classLcapName}.${field.name}(getBooleanValue(interpolatedTrimmed(value, "$fieldTagName"), "$fieldTagName", parser, ${field.defaultValue}));
#else #else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
#end #end
} }
#end #end
#end #end
else else {
{ checkUnknownAttribute(parser, name, tagName, strict);
checkUnknownAttribute( parser, name, tagName, strict );
} }
} }
Set<String> parsed = new HashSet<>(); Set<String> parsed = new HashSet<>();
while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG ) while ((strict ? parser.nextTag() : nextTag(parser)) == XmlPullParser.START_TAG) {
{ String childName = unalias(parser.getName());
String childName = unalias( parser.getName() ); if (!parsed.add(childName)) {
if ( !parsed.add( childName ) ) throw new XmlPullParserException("Duplicated tag: '" + childName + "'", parser, null);
{
throw new XmlPullParserException( "Duplicated tag: '" + childName + "'", parser, null );
} }
switch ( childName ) switch (childName) {
{
#set( $ift = "if" ) #set( $ift = "if" )
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#if ( ! $Helper.xmlFieldMetadata( $field ).attribute && ! $Helper.xmlFieldMetadata( $field ).transient ) #if ( ! $Helper.xmlFieldMetadata( $field ).attribute && ! $Helper.xmlFieldMetadata( $field ).transient )
@ -266,62 +234,54 @@ public class ${className}
#set ( $fieldTagName = $field.name ) #set ( $fieldTagName = $field.name )
#end #end
#set ( $fieldCapName = $Helper.capitalise( $field.name ) ) #set ( $fieldCapName = $Helper.capitalise( $field.name ) )
case "${fieldTagName}": case "${fieldTagName}": {
{
#if ( $field.type == "String" ) #if ( $field.type == "String" )
${classLcapName}.${field.name}( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ) ); ${classLcapName}.${field.name}(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"));
break; break;
#elseif ( $field.type == "boolean" || $field.type == "Boolean" ) #elseif ( $field.type == "boolean" || $field.type == "Boolean" )
${classLcapName}.${field.name}( getBooleanValue( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ), "${fieldTagName}", parser, ${field.defaultValue} ) ); ${classLcapName}.${field.name}(getBooleanValue(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"), "${fieldTagName}", parser, ${field.defaultValue}));
break; break;
#elseif ( $field.type == "int" ) #elseif ( $field.type == "int" )
${classLcapName}.${field.name}( getIntegerValue( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ), "${fieldTagName}", parser, strict, ${field.defaultValue} ) ); ${classLcapName}.${field.name}(getIntegerValue(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"), "${fieldTagName}", parser, strict, ${field.defaultValue}));
break; break;
#elseif ( $field.type == "DOM" ) #elseif ( $field.type == "DOM" )
${classLcapName}.${field.name}( XmlNodeBuilder.build( parser, true ) ); ${classLcapName}.${field.name}(XmlNodeBuilder.build(parser, true));
break; break;
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
List<String> ${field.name} = new ArrayList<>(); List<String> ${field.name} = new ArrayList<>();
while ( parser.nextTag() == XmlPullParser.START_TAG ) while (parser.nextTag() == XmlPullParser.START_TAG) {
{ if ("${Helper.singular($fieldTagName)}".equals(parser.getName())) {
if ( "${Helper.singular($fieldTagName)}".equals( parser.getName() ) ) ${field.name}.add(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"));
{
${field.name}.add( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ) );
} }
else else {
{ checkUnknownElement(parser, strict);
checkUnknownElement( parser, strict );
} }
} }
${classLcapName}.${field.name}( ${field.name} ); ${classLcapName}.${field.name}(${field.name});
break; break;
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> ${field.name} = new LinkedHashMap<>(); Map<String, String> ${field.name} = new LinkedHashMap<>();
while ( parser.nextTag() == XmlPullParser.START_TAG ) while (parser.nextTag() == XmlPullParser.START_TAG) {
{
String key = parser.getName(); String key = parser.getName();
String value = parser.nextText().trim(); String value = parser.nextText().trim();
${field.name}.put( key, value ); ${field.name}.put(key, value);
} }
${classLcapName}.${field.name}( ${field.name} ); ${classLcapName}.${field.name}(${field.name});
break; break;
#elseif ( $field.to && $field.multiplicity == "1" ) #elseif ( $field.to && $field.multiplicity == "1" )
${classLcapName}.${field.name}( parse${field.toClass.name}( parser, strict ) ); ${classLcapName}.${field.name}(parse${field.toClass.name}(parser, strict));
break; break;
#elseif ( $field.to && $field.multiplicity == "*" ) #elseif ( $field.to && $field.multiplicity == "*" )
List<$field.to> ${field.name} = new ArrayList<>(); List<$field.to> ${field.name} = new ArrayList<>();
while ( parser.nextTag() == XmlPullParser.START_TAG ) while (parser.nextTag() == XmlPullParser.START_TAG) {
{ if ("${Helper.singular($fieldTagName)}".equals(parser.getName())) {
if ( "${Helper.singular($fieldTagName)}".equals( parser.getName() ) ) ${field.name}.add(parse${field.toClass.name}(parser, strict));
{
${field.name}.add( parse${field.toClass.name}( parser, strict ) );
} }
else else {
{ checkUnknownElement(parser, strict);
checkUnknownElement( parser, strict );
} }
} }
${classLcapName}.${field.name}( ${field.name} ); ${classLcapName}.${field.name}(${field.name});
break; break;
#else #else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
@ -331,15 +291,14 @@ public class ${className}
#set( $ift = "else if" ) #set( $ift = "else if" )
#end #end
#end #end
default: default: {
{ checkUnknownElement(parser, strict);
checkUnknownElement( parser, strict );
break; break;
} }
} }
} }
#if ( $class == $root ) #if ( $class == $root )
${classLcapName}.modelEncoding( parser.getInputEncoding() ); ${classLcapName}.modelEncoding(parser.getInputEncoding());
#end #end
return ${classLcapName}.build(); return ${classLcapName}.build();
} }
@ -347,10 +306,8 @@ public class ${className}
#end #end
#end #end
private String unalias( String tagName ) private String unalias(String tagName) {
{ switch (tagName) {
switch ( tagName )
{
#set( $aliases = { } ) #set( $aliases = { } )
#foreach( $class in $model.allClasses ) #foreach( $class in $model.allClasses )
#foreach ( $field in $class.allFields ) #foreach ( $field in $class.allFields )
@ -379,15 +336,12 @@ public class ${className}
* any. * any.
* @throws IOException IOException if any. * @throws IOException IOException if any.
*/ */
private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict ) private void checkUnknownAttribute(XmlPullParser parser, String attribute, String tagName, boolean strict) throws XmlPullParserException, IOException {
throws XmlPullParserException, IOException
{
// strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too // strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
if ( strict ) if (strict) {
{ throw new XmlPullParserException("Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null);
throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
} }
} //-- void checkUnknownAttribute( XmlPullParser, String, String, boolean ) } //-- void checkUnknownAttribute(XmlPullParser, String, String, boolean)
/** /**
* Method checkUnknownElement. * Method checkUnknownElement.
@ -398,27 +352,21 @@ public class ${className}
* any. * any.
* @throws IOException IOException if any. * @throws IOException IOException if any.
*/ */
private void checkUnknownElement( XmlPullParser parser, boolean strict ) private void checkUnknownElement(XmlPullParser parser, boolean strict) throws XmlPullParserException, IOException {
throws XmlPullParserException, IOException if (strict) {
{ throw new XmlPullParserException("Unrecognised tag: '" + parser.getName() + "'", parser, null);
if ( strict )
{
throw new XmlPullParserException( "Unrecognised tag: '" + parser.getName() + "'", parser, null );
} }
for ( int unrecognizedTagCount = 1; unrecognizedTagCount > 0; ) for (int unrecognizedTagCount = 1; unrecognizedTagCount > 0;) {
{
int eventType = parser.next(); int eventType = parser.next();
if ( eventType == XmlPullParser.START_TAG ) if (eventType == XmlPullParser.START_TAG) {
{
unrecognizedTagCount++; unrecognizedTagCount++;
} }
else if ( eventType == XmlPullParser.END_TAG ) else if (eventType == XmlPullParser.END_TAG) {
{
unrecognizedTagCount--; unrecognizedTagCount--;
} }
} }
} //-- void checkUnknownElement( XmlPullParser, boolean ) } //-- void checkUnknownElement(XmlPullParser, boolean)
/** /**
* Method getTrimmedValue. * Method getTrimmedValue.
@ -426,14 +374,12 @@ public class ${className}
* @param s a s object. * @param s a s object.
* @return String * @return String
*/ */
private String getTrimmedValue( String s ) private String getTrimmedValue(String s) {
{ if (s != null) {
if ( s != null )
{
s = s.trim(); s = s.trim();
} }
return s; return s;
} //-- String getTrimmedValue( String ) } //-- String getTrimmedValue(String)
/** /**
* Method interpolatedTrimmed. * Method interpolatedTrimmed.
@ -442,10 +388,9 @@ public class ${className}
* @param context a context object. * @param context a context object.
* @return String * @return String
*/ */
private String interpolatedTrimmed( String value, String context ) private String interpolatedTrimmed(String value, String context) {
{ return getTrimmedValue(contentTransformer.transform(value, context));
return getTrimmedValue( contentTransformer.transform( value, context ) ); } //-- String interpolatedTrimmed(String, String)
} //-- String interpolatedTrimmed( String, String )
/** /**
* Method nextTag. * Method nextTag.
@ -456,20 +401,16 @@ public class ${className}
* any. * any.
* @return int * @return int
*/ */
private int nextTag( XmlPullParser parser ) private int nextTag(XmlPullParser parser) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException
{
int eventType = parser.next(); int eventType = parser.next();
if ( eventType == XmlPullParser.TEXT ) if (eventType == XmlPullParser.TEXT) {
{
eventType = parser.next(); eventType = parser.next();
} }
if ( eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG ) if (eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG) {
{ throw new XmlPullParserException("expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null);
throw new XmlPullParserException( "expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null );
} }
return eventType; return eventType;
} //-- int nextTag( XmlPullParser ) } //-- int nextTag(XmlPullParser)
#foreach ( $class in $model.allClasses ) #foreach ( $class in $model.allClasses )
#foreach ( $field in $class.getFields($version) ) #foreach ( $field in $class.getFields($version) )
@ -492,15 +433,12 @@ public class ${className}
* any. * any.
* @return boolean * @return boolean
*/ */
private boolean getBooleanValue( String s, String attribute, XmlPullParser parser, boolean defaultValue ) private boolean getBooleanValue(String s, String attribute, XmlPullParser parser, boolean defaultValue) throws XmlPullParserException {
throws XmlPullParserException if (s != null && s.length() != 0) {
{ return Boolean.valueOf(s).booleanValue();
if ( s != null && s.length() != 0 )
{
return Boolean.valueOf( s ).booleanValue();
} }
return defaultValue; return defaultValue;
} //-- boolean getBooleanValue( String, String, XmlPullParser, String ) } //-- boolean getBooleanValue(String, String, XmlPullParser, String)
#end #end
#if ( $hasIntegerField ) #if ( $hasIntegerField )
@ -515,29 +453,22 @@ public class ${className}
* any. * any.
* @return int * @return int
*/ */
private int getIntegerValue( String s, String attribute, XmlPullParser parser, boolean strict, int defaultValue ) private int getIntegerValue(String s, String attribute, XmlPullParser parser, boolean strict, int defaultValue) throws XmlPullParserException {
throws XmlPullParserException if (s != null) {
{ try {
if ( s != null ) return Integer.valueOf(s).intValue();
{
try
{
return Integer.valueOf( s ).intValue();
} }
catch ( NumberFormatException nfe ) catch (NumberFormatException nfe) {
{ if (strict) {
if ( strict ) throw new XmlPullParserException("Unable to parse element '" + attribute + "', must be an integer", parser, nfe);
{
throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be an integer", parser, nfe );
} }
} }
} }
return defaultValue; return defaultValue;
} //-- int getIntegerValue( String, String, XmlPullParser, boolean ) } //-- int getIntegerValue(String, String, XmlPullParser, boolean)
#end #end
public static interface ContentTransformer public static interface ContentTransformer {
{
/** /**
* Interpolate the value read from the xpp3 document * Interpolate the value read from the xpp3 document
* @param source The source value * @param source The source value
@ -545,7 +476,7 @@ public class ${className}
* log stuff. * log stuff.
* @return The interpolated value. * @return The interpolated value.
*/ */
String transform( String source, String fieldName ); String transform(String source, String fieldName);
} }
} }

View File

@ -59,19 +59,17 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParser;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException; import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
@Generated @Generated
public class ${className} public class ${className} {
{
private boolean addDefaultEntities = true; private boolean addDefaultEntities = true;
private final ContentTransformer contentTransformer; private final ContentTransformer contentTransformer;
public ${className}() public ${className}() {
{ this((s, f) -> s);
this( ( s, f ) -> s );
} }
public ${className}( ContentTransformer contentTransformer ) public ${className}(ContentTransformer contentTransformer) {
{
this.contentTransformer = contentTransformer; this.contentTransformer = contentTransformer;
} }
@ -86,19 +84,16 @@ public class ${className}
* any. * any.
* @return boolean * @return boolean
*/ */
private boolean checkFieldWithDuplicate( XmlPullParser parser, String tagName, String alias, Set<String> parsed ) private boolean checkFieldWithDuplicate(XmlPullParser parser, String tagName, String alias, Set<String> parsed)
throws XmlPullParserException throws XmlPullParserException {
{ if (!(parser.getName().equals(tagName) || parser.getName().equals(alias))) {
if ( !( parser.getName().equals( tagName ) || parser.getName().equals( alias ) ) )
{
return false; return false;
} }
if ( !parsed.add( tagName ) ) if (!parsed.add(tagName)) {
{ throw new XmlPullParserException("Duplicated tag: '" + tagName + "'", parser, null);
throw new XmlPullParserException( "Duplicated tag: '" + tagName + "'", parser, null );
} }
return true; return true;
} //-- boolean checkFieldWithDuplicate( XmlPullParser, String, String, Set<String> ) } //-- boolean checkFieldWithDuplicate(XmlPullParser, String, String, Set<String>)
/** /**
* Method checkUnknownAttribute. * Method checkUnknownAttribute.
@ -111,15 +106,13 @@ public class ${className}
* any. * any.
* @throws IOException IOException if any. * @throws IOException IOException if any.
*/ */
private void checkUnknownAttribute( XmlPullParser parser, String attribute, String tagName, boolean strict ) private void checkUnknownAttribute(XmlPullParser parser, String attribute, String tagName, boolean strict)
throws XmlPullParserException, IOException throws XmlPullParserException, IOException {
{
// strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too // strictXmlAttributes = true for model: if strict == true, not only elements are checked but attributes too
if ( strict ) if (strict) {
{ throw new XmlPullParserException("Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null);
throw new XmlPullParserException( "Unknown attribute '" + attribute + "' for tag '" + tagName + "'", parser, null );
} }
} //-- void checkUnknownAttribute( XmlPullParser, String, String, boolean ) } //-- void checkUnknownAttribute(XmlPullParser, String, String, boolean)
/** /**
* Method checkUnknownElement. * Method checkUnknownElement.
@ -130,35 +123,28 @@ public class ${className}
* any. * any.
* @throws IOException IOException if any. * @throws IOException IOException if any.
*/ */
private void checkUnknownElement( XmlPullParser parser, boolean strict ) private void checkUnknownElement(XmlPullParser parser, boolean strict)
throws XmlPullParserException, IOException throws XmlPullParserException, IOException {
{ if (strict) {
if ( strict ) throw new XmlPullParserException("Unrecognised tag: '" + parser.getName() + "'", parser, null);
{
throw new XmlPullParserException( "Unrecognised tag: '" + parser.getName() + "'", parser, null );
} }
for ( int unrecognizedTagCount = 1; unrecognizedTagCount > 0; ) for (int unrecognizedTagCount = 1; unrecognizedTagCount > 0;) {
{
int eventType = parser.next(); int eventType = parser.next();
if ( eventType == XmlPullParser.START_TAG ) if (eventType == XmlPullParser.START_TAG) {
{
unrecognizedTagCount++; unrecognizedTagCount++;
} } else if (eventType == XmlPullParser.END_TAG) {
else if ( eventType == XmlPullParser.END_TAG )
{
unrecognizedTagCount--; unrecognizedTagCount--;
} }
} }
} //-- void checkUnknownElement( XmlPullParser, boolean ) } //-- void checkUnknownElement(XmlPullParser, boolean)
/** /**
* Returns the state of the "add default entities" flag. * Returns the state of the "add default entities" flag.
* *
* @return boolean * @return boolean
*/ */
public boolean getAddDefaultEntities() public boolean getAddDefaultEntities() {
{
return addDefaultEntities; return addDefaultEntities;
} //-- boolean getAddDefaultEntities() } //-- boolean getAddDefaultEntities()
@ -172,11 +158,10 @@ public class ${className}
* any. * any.
* @return boolean * @return boolean
*/ */
private boolean getBooleanValue( String s, String attribute, XmlPullParser parser ) private boolean getBooleanValue(String s, String attribute, XmlPullParser parser)
throws XmlPullParserException throws XmlPullParserException {
{ return getBooleanValue(s, attribute, parser, false);
return getBooleanValue( s, attribute, parser, false ); } //-- boolean getBooleanValue(String, String, XmlPullParser)
} //-- boolean getBooleanValue( String, String, XmlPullParser )
/** /**
* Method getBooleanValue. * Method getBooleanValue.
@ -189,15 +174,13 @@ public class ${className}
* any. * any.
* @return boolean * @return boolean
*/ */
private boolean getBooleanValue( String s, String attribute, XmlPullParser parser, boolean defaultValue ) private boolean getBooleanValue(String s, String attribute, XmlPullParser parser, boolean defaultValue)
throws XmlPullParserException throws XmlPullParserException {
{ if (s != null && s.length() != 0) {
if ( s != null && s.length() != 0 ) return Boolean.valueOf(s).booleanValue();
{
return Boolean.valueOf( s ).booleanValue();
} }
return defaultValue; return defaultValue;
} //-- boolean getBooleanValue( String, String, XmlPullParser, String ) } //-- boolean getBooleanValue(String, String, XmlPullParser, String)
/** /**
* Method getByteValue. * Method getByteValue.
@ -210,25 +193,19 @@ public class ${className}
* any. * any.
* @return byte * @return byte
*/ */
private byte getByteValue( String s, String attribute, XmlPullParser parser, boolean strict ) private byte getByteValue(String s, String attribute, XmlPullParser parser, boolean strict)
throws XmlPullParserException throws XmlPullParserException {
{ if (s != null) {
if ( s != null ) try {
{ return Byte.valueOf(s).byteValue();
try } catch (NumberFormatException nfe) {
{ if (strict) {
return Byte.valueOf( s ).byteValue(); throw new XmlPullParserException("Unable to parse element '" + attribute + "', must be a byte", parser, nfe);
}
catch ( NumberFormatException nfe )
{
if ( strict )
{
throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a byte", parser, nfe );
} }
} }
} }
return 0; return 0;
} //-- byte getByteValue( String, String, XmlPullParser, boolean ) } //-- byte getByteValue(String, String, XmlPullParser, boolean)
/** /**
* Method getCharacterValue. * Method getCharacterValue.
@ -240,15 +217,13 @@ public class ${className}
* any. * any.
* @return char * @return char
*/ */
private char getCharacterValue( String s, String attribute, XmlPullParser parser ) private char getCharacterValue(String s, String attribute, XmlPullParser parser)
throws XmlPullParserException throws XmlPullParserException {
{ if (s != null) {
if ( s != null ) return s.charAt(0);
{
return s.charAt( 0 );
} }
return 0; return 0;
} //-- char getCharacterValue( String, String, XmlPullParser ) } //-- char getCharacterValue(String, String, XmlPullParser)
/** /**
* Method getDateValue. * Method getDateValue.
@ -260,11 +235,10 @@ public class ${className}
* any. * any.
* @return Date * @return Date
*/ */
private Date getDateValue( String s, String attribute, XmlPullParser parser ) private Date getDateValue(String s, String attribute, XmlPullParser parser)
throws XmlPullParserException throws XmlPullParserException {
{ return getDateValue(s, attribute, null, parser);
return getDateValue( s, attribute, null, parser ); } //-- Date getDateValue(String, String, XmlPullParser)
} //-- Date getDateValue( String, String, XmlPullParser )
/** /**
* Method getDateValue. * Method getDateValue.
@ -277,42 +251,30 @@ public class ${className}
* any. * any.
* @return Date * @return Date
*/ */
private Date getDateValue( String s, String attribute, String dateFormat, XmlPullParser parser ) private Date getDateValue(String s, String attribute, String dateFormat, XmlPullParser parser)
throws XmlPullParserException throws XmlPullParserException {
{ if (s != null) {
if ( s != null )
{
String effectiveDateFormat = dateFormat; String effectiveDateFormat = dateFormat;
if ( dateFormat == null ) if (dateFormat == null) {
{
effectiveDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS"; effectiveDateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS";
} }
if ( "long".equals( effectiveDateFormat ) ) if ("long".equals(effectiveDateFormat)) {
{ try {
try return new java.util.Date(Long.parseLong(s));
{ } catch (NumberFormatException e) {
return new java.util.Date( Long.parseLong( s ) ); throw new XmlPullParserException(e.getMessage(), parser, e);
} }
catch ( NumberFormatException e ) } else {
{ try {
throw new XmlPullParserException( e.getMessage(), parser, e ); DateFormat dateParser = new java.text.SimpleDateFormat(effectiveDateFormat, java.util.Locale.US);
} return dateParser.parse(s);
} } catch (java.text.ParseException e) {
else throw new XmlPullParserException(e.getMessage(), parser, e);
{
try
{
DateFormat dateParser = new java.text.SimpleDateFormat( effectiveDateFormat, java.util.Locale.US );
return dateParser.parse( s );
}
catch ( java.text.ParseException e )
{
throw new XmlPullParserException( e.getMessage(), parser, e );
} }
} }
} }
return null; return null;
} //-- Date getDateValue( String, String, String, XmlPullParser ) } //-- Date getDateValue(String, String, String, XmlPullParser)
/** /**
* Method getDoubleValue. * Method getDoubleValue.
@ -325,25 +287,19 @@ public class ${className}
* any. * any.
* @return double * @return double
*/ */
private double getDoubleValue( String s, String attribute, XmlPullParser parser, boolean strict ) private double getDoubleValue(String s, String attribute, XmlPullParser parser, boolean strict)
throws XmlPullParserException throws XmlPullParserException {
{ if (s != null) {
if ( s != null ) try {
{ return Double.valueOf(s).doubleValue();
try } catch (NumberFormatException nfe) {
{ if (strict) {
return Double.valueOf( s ).doubleValue(); throw new XmlPullParserException("Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe);
}
catch ( NumberFormatException nfe )
{
if ( strict )
{
throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
} }
} }
} }
return 0; return 0;
} //-- double getDoubleValue( String, String, XmlPullParser, boolean ) } //-- double getDoubleValue(String, String, XmlPullParser, boolean)
/** /**
* Method getFloatValue. * Method getFloatValue.
@ -356,25 +312,19 @@ public class ${className}
* any. * any.
* @return float * @return float
*/ */
private float getFloatValue( String s, String attribute, XmlPullParser parser, boolean strict ) private float getFloatValue(String s, String attribute, XmlPullParser parser, boolean strict)
throws XmlPullParserException throws XmlPullParserException {
{ if (s != null) {
if ( s != null ) try {
{ return Float.valueOf(s).floatValue();
try } catch (NumberFormatException nfe) {
{ if (strict) {
return Float.valueOf( s ).floatValue(); throw new XmlPullParserException("Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe);
}
catch ( NumberFormatException nfe )
{
if ( strict )
{
throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a floating point number", parser, nfe );
} }
} }
} }
return 0; return 0;
} //-- float getFloatValue( String, String, XmlPullParser, boolean ) } //-- float getFloatValue(String, String, XmlPullParser, boolean)
/** /**
* Method getIntegerValue. * Method getIntegerValue.
@ -386,11 +336,10 @@ public class ${className}
* any. * any.
* @return int * @return int
*/ */
private int getIntegerValue( String s, String attribute, XmlPullParser parser, boolean strict ) private int getIntegerValue(String s, String attribute, XmlPullParser parser, boolean strict)
throws XmlPullParserException throws XmlPullParserException {
{ return getIntegerValue(s, attribute, parser, strict, 0);
return getIntegerValue( s, attribute, parser, strict, 0 ); } //-- int getBooleanValue(String, String, XmlPullParser)
} //-- int getBooleanValue( String, String, XmlPullParser )
/** /**
* Method getIntegerValue. * Method getIntegerValue.
@ -403,25 +352,19 @@ public class ${className}
* any. * any.
* @return int * @return int
*/ */
private int getIntegerValue( String s, String attribute, XmlPullParser parser, boolean strict, int defaultValue ) private int getIntegerValue(String s, String attribute, XmlPullParser parser, boolean strict, int defaultValue)
throws XmlPullParserException throws XmlPullParserException {
{ if (s != null) {
if ( s != null ) try {
{ return Integer.valueOf(s).intValue();
try } catch (NumberFormatException nfe) {
{ if (strict) {
return Integer.valueOf( s ).intValue(); throw new XmlPullParserException("Unable to parse element '" + attribute + "', must be an integer", parser, nfe);
}
catch ( NumberFormatException nfe )
{
if ( strict )
{
throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be an integer", parser, nfe );
} }
} }
} }
return defaultValue; return defaultValue;
} //-- int getIntegerValue( String, String, XmlPullParser, boolean, int ) } //-- int getIntegerValue(String, String, XmlPullParser, boolean, int)
/** /**
* Method getLongValue. * Method getLongValue.
@ -434,25 +377,19 @@ public class ${className}
* any. * any.
* @return long * @return long
*/ */
private long getLongValue( String s, String attribute, XmlPullParser parser, boolean strict ) private long getLongValue(String s, String attribute, XmlPullParser parser, boolean strict)
throws XmlPullParserException throws XmlPullParserException {
{ if (s != null) {
if ( s != null ) try {
{ return Long.valueOf(s).longValue();
try } catch (NumberFormatException nfe) {
{ if (strict) {
return Long.valueOf( s ).longValue(); throw new XmlPullParserException("Unable to parse element '" + attribute + "', must be a long integer", parser, nfe);
}
catch ( NumberFormatException nfe )
{
if ( strict )
{
throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a long integer", parser, nfe );
} }
} }
} }
return 0; return 0;
} //-- long getLongValue( String, String, XmlPullParser, boolean ) } //-- long getLongValue(String, String, XmlPullParser, boolean)
/** /**
* Method getRequiredAttributeValue. * Method getRequiredAttributeValue.
@ -465,18 +402,15 @@ public class ${className}
* any. * any.
* @return String * @return String
*/ */
private String getRequiredAttributeValue( String s, String attribute, XmlPullParser parser, boolean strict ) private String getRequiredAttributeValue(String s, String attribute, XmlPullParser parser, boolean strict)
throws XmlPullParserException throws XmlPullParserException {
{ if (s == null) {
if ( s == null ) if (strict) {
{ throw new XmlPullParserException("Missing required value for attribute '" + attribute + "'", parser, null);
if ( strict )
{
throw new XmlPullParserException( "Missing required value for attribute '" + attribute + "'", parser, null );
} }
} }
return s; return s;
} //-- String getRequiredAttributeValue( String, String, XmlPullParser, boolean ) } //-- String getRequiredAttributeValue(String, String, XmlPullParser, boolean)
/** /**
* Method getShortValue. * Method getShortValue.
@ -489,25 +423,19 @@ public class ${className}
* any. * any.
* @return short * @return short
*/ */
private short getShortValue( String s, String attribute, XmlPullParser parser, boolean strict ) private short getShortValue(String s, String attribute, XmlPullParser parser, boolean strict)
throws XmlPullParserException throws XmlPullParserException {
{ if (s != null) {
if ( s != null ) try {
{ return Short.valueOf(s).shortValue();
try } catch (NumberFormatException nfe) {
{ if (strict) {
return Short.valueOf( s ).shortValue(); throw new XmlPullParserException("Unable to parse element '" + attribute + "', must be a short integer", parser, nfe);
}
catch ( NumberFormatException nfe )
{
if ( strict )
{
throw new XmlPullParserException( "Unable to parse element '" + attribute + "', must be a short integer", parser, nfe );
} }
} }
} }
return 0; return 0;
} //-- short getShortValue( String, String, XmlPullParser, boolean ) } //-- short getShortValue(String, String, XmlPullParser, boolean)
/** /**
* Method getTrimmedValue. * Method getTrimmedValue.
@ -515,14 +443,12 @@ public class ${className}
* @param s a s object. * @param s a s object.
* @return String * @return String
*/ */
private String getTrimmedValue( String s ) private String getTrimmedValue(String s) {
{ if (s != null) {
if ( s != null )
{
s = s.trim(); s = s.trim();
} }
return s; return s;
} //-- String getTrimmedValue( String ) } //-- String getTrimmedValue(String)
/** /**
* Method interpolatedTrimmed. * Method interpolatedTrimmed.
@ -531,10 +457,9 @@ public class ${className}
* @param context a context object. * @param context a context object.
* @return String * @return String
*/ */
private String interpolatedTrimmed( String value, String context ) private String interpolatedTrimmed(String value, String context) {
{ return getTrimmedValue(contentTransformer.transform(value, context));
return getTrimmedValue( contentTransformer.transform( value, context ) ); } //-- String interpolatedTrimmed(String, String)
} //-- String interpolatedTrimmed( String, String )
/** /**
* Method nextTag. * Method nextTag.
@ -545,20 +470,16 @@ public class ${className}
* any. * any.
* @return int * @return int
*/ */
private int nextTag( XmlPullParser parser ) private int nextTag(XmlPullParser parser) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException
{
int eventType = parser.next(); int eventType = parser.next();
if ( eventType == XmlPullParser.TEXT ) if (eventType == XmlPullParser.TEXT) {
{
eventType = parser.next(); eventType = parser.next();
} }
if ( eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG ) if (eventType != XmlPullParser.START_TAG && eventType != XmlPullParser.END_TAG) {
{ throw new XmlPullParserException("expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null);
throw new XmlPullParserException( "expected START_TAG or END_TAG not " + XmlPullParser.TYPES[eventType], parser, null );
} }
return eventType; return eventType;
} //-- int nextTag( XmlPullParser ) } //-- int nextTag(XmlPullParser)
/** /**
* @see ReaderFactory#newXmlReader * @see ReaderFactory#newXmlReader
@ -570,16 +491,11 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( Reader reader, boolean strict ) public ${root.name} read(Reader reader, boolean strict) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser();
{ parser.setInput(reader);
XmlPullParser parser = addDefaultEntities ? new MXParser(EntityReplacementMap.defaultEntityReplacementMap) : new MXParser( ); return read(parser, strict);
} //-- ${root.name} read(Reader, boolean)
parser.setInput( reader );
return read( parser, strict );
} //-- ${root.name} read( Reader, boolean )
/** /**
* @see ReaderFactory#newXmlReader * @see ReaderFactory#newXmlReader
@ -590,11 +506,9 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( Reader reader ) public ${root.name} read(Reader reader) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException return read(reader, true);
{ } //-- ${root.name} read(Reader)
return read( reader, true );
} //-- ${root.name} read( Reader )
/** /**
* Method read. * Method read.
@ -606,11 +520,9 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( InputStream in, boolean strict ) public ${root.name} read(InputStream in, boolean strict) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException return read(ReaderFactory.newXmlReader(in), strict);
{ } //-- ${root.name} read(InputStream, boolean)
return read( ReaderFactory.newXmlReader( in ), strict );
} //-- ${root.name} read( InputStream, boolean )
/** /**
* Method read. * Method read.
@ -621,11 +533,9 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( InputStream in ) public ${root.name} read(InputStream in) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException return read(ReaderFactory.newXmlReader(in));
{ } //-- ${root.name} read(InputStream)
return read( ReaderFactory.newXmlReader( in ) );
} //-- ${root.name} read( InputStream )
/** /**
* Method read. * Method read.
@ -637,36 +547,28 @@ public class ${className}
* any. * any.
* @return ${root.name} * @return ${root.name}
*/ */
public ${root.name} read( XmlPullParser parser, boolean strict ) public ${root.name} read(XmlPullParser parser, boolean strict) throws IOException, XmlPullParserException {
throws IOException, XmlPullParserException
{
$rootUcapName $rootLcapName = null; $rootUcapName $rootLcapName = null;
int eventType = parser.getEventType(); int eventType = parser.getEventType();
boolean parsed = false; boolean parsed = false;
while ( eventType != XmlPullParser.END_DOCUMENT ) while (eventType != XmlPullParser.END_DOCUMENT) {
{ if (eventType == XmlPullParser.START_TAG) {
if ( eventType == XmlPullParser.START_TAG ) if (strict && ! "${rootTag}".equals(parser.getName())) {
{ throw new XmlPullParserException("Expected root element '${rootTag}' but found '" + parser.getName() + "'", parser, null);
if ( strict && ! "${rootTag}".equals( parser.getName() ) ) } else if (parsed) {
{
throw new XmlPullParserException( "Expected root element '${rootTag}' but found '" + parser.getName() + "'", parser, null );
}
else if ( parsed )
{
// fallback, already expected a XmlPullParserException due to invalid XML // fallback, already expected a XmlPullParserException due to invalid XML
throw new XmlPullParserException( "Duplicated tag: '${rootTag}'", parser, null ); throw new XmlPullParserException("Duplicated tag: '${rootTag}'", parser, null);
} }
$rootLcapName = parse${rootUcapName}( parser, strict ); $rootLcapName = parse${rootUcapName}(parser, strict);
parsed = true; parsed = true;
} }
eventType = parser.next(); eventType = parser.next();
} }
if ( parsed ) if (parsed) {
{
return $rootLcapName; return $rootLcapName;
} }
throw new XmlPullParserException( "Expected root element '${rootTag}' but found no element at all: invalid XML document", parser, null ); throw new XmlPullParserException("Expected root element '${rootTag}' but found no element at all: invalid XML document", parser, null);
} //-- ${root.name} read( XmlPullParser, boolean ) } //-- ${root.name} read(XmlPullParser, boolean)
#foreach ( $class in $model.allClasses ) #foreach ( $class in $model.allClasses )
#if ( $class.name != "InputSource" && $class.name != "InputLocation" ) #if ( $class.name != "InputSource" && $class.name != "InputLocation" )
@ -677,44 +579,37 @@ public class ${className}
#foreach ( $cl in $ancestors ) #foreach ( $cl in $ancestors )
#set ( $dummy = $allFields.addAll( $cl.getFields($version) ) ) #set ( $dummy = $allFields.addAll( $cl.getFields($version) ) )
#end #end
private ${classUcapName} parse${classUcapName}( XmlPullParser parser, boolean strict ) private ${classUcapName} parse${classUcapName}(XmlPullParser parser, boolean strict)
throws IOException, XmlPullParserException throws IOException, XmlPullParserException {
{
String tagName = parser.getName(); String tagName = parser.getName();
${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder( true ); ${classUcapName}.Builder ${classLcapName} = ${classUcapName}.newBuilder(true);
for ( int i = parser.getAttributeCount() - 1; i >= 0; i-- ) for (int i = parser.getAttributeCount() - 1; i >= 0; i--) {
{ String name = parser.getAttributeName(i);
String name = parser.getAttributeName( i ); String value = parser.getAttributeValue(i);
String value = parser.getAttributeValue( i ); if (name.indexOf(':') >= 0) {
if ( name.indexOf( ':' ) >= 0 )
{
// just ignore attributes with non-default namespace (for example: xmlns:xsi) // just ignore attributes with non-default namespace (for example: xmlns:xsi)
}
#if ( $class == $root ) #if ( $class == $root )
else if ( "xmlns".equals( name ) ) } else if ("xmlns".equals(name)) {
{
// ignore xmlns attribute in root class, which is a reserved attribute name // ignore xmlns attribute in root class, which is a reserved attribute name
}
#end #end
}
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#if ( $Helper.xmlFieldMetadata( $field ).attribute ) #if ( $Helper.xmlFieldMetadata( $field ).attribute )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName ) #set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#set ( $fieldCapName = $Helper.capitalise( $field.name ) ) #set ( $fieldCapName = $Helper.capitalise( $field.name ) )
else if ( "$fieldTagName".equals( name ) ) else if ("$fieldTagName".equals(name)) {
{
#if ( $field.type == "String" ) #if ( $field.type == "String" )
${classLcapName}.${field.name}( interpolatedTrimmed( value, "$fieldTagName" ) ); ${classLcapName}.${field.name}(interpolatedTrimmed(value, "$fieldTagName"));
#elseif ( $field.type == "boolean" || $field.type == "Boolean" ) #elseif ( $field.type == "boolean" || $field.type == "Boolean" )
${classLcapName}.${field.name}( getBooleanValue( interpolatedTrimmed( value, "$fieldTagName" ), "$fieldTagName", parser, ${field.defaultValue} ) ); ${classLcapName}.${field.name}(getBooleanValue(interpolatedTrimmed(value, "$fieldTagName"), "$fieldTagName", parser, ${field.defaultValue}));
#else #else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
#end #end
} }
#end #end
#end #end
else else {
{ checkUnknownAttribute(parser, name, tagName, strict);
checkUnknownAttribute( parser, name, tagName, strict );
} }
} }
Set<String> parsed = new HashSet<>(); Set<String> parsed = new HashSet<>();
@ -723,11 +618,9 @@ public class ${className}
List<$field.to> ${field.name} = new ArrayList<>(); List<$field.to> ${field.name} = new ArrayList<>();
#end #end
#end #end
while ( ( strict ? parser.nextTag() : nextTag( parser ) ) == XmlPullParser.START_TAG ) while ((strict ? parser.nextTag() : nextTag(parser)) == XmlPullParser.START_TAG) {
{ String childName = checkDuplicate(parser.getName(), parser, parsed);
String childName = checkDuplicate( parser.getName(), parser, parsed ); switch (childName) {
switch ( childName )
{
#set( $ift = "if" ) #set( $ift = "if" )
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#if ( ! $Helper.xmlFieldMetadata( $field ).attribute && ! $Helper.xmlFieldMetadata( $field ).transient ) #if ( ! $Helper.xmlFieldMetadata( $field ).attribute && ! $Helper.xmlFieldMetadata( $field ).transient )
@ -739,65 +632,55 @@ public class ${className}
#set ( $fieldTagName = $Helper.singular( $fieldTagName ) ) #set ( $fieldTagName = $Helper.singular( $fieldTagName ) )
#end #end
#set ( $fieldCapName = $Helper.capitalise( $field.name ) ) #set ( $fieldCapName = $Helper.capitalise( $field.name ) )
case "${fieldTagName}": case "${fieldTagName}": {
{
#if ( $field.type == "String" ) #if ( $field.type == "String" )
${classLcapName}.${field.name}( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ) ); ${classLcapName}.${field.name}(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"));
break; break;
#elseif ( $field.type == "boolean" || $field.type == "Boolean" ) #elseif ( $field.type == "boolean" || $field.type == "Boolean" )
${classLcapName}.${field.name}( getBooleanValue( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ), "${fieldTagName}", parser, ${field.defaultValue} ) ); ${classLcapName}.${field.name}(getBooleanValue(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"), "${fieldTagName}", parser, ${field.defaultValue}));
break; break;
#elseif ( $field.type == "int" ) #elseif ( $field.type == "int" )
${classLcapName}.${field.name}( getIntegerValue( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ), "${fieldTagName}", parser, strict, ${field.defaultValue} ) ); ${classLcapName}.${field.name}(getIntegerValue(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"), "${fieldTagName}", parser, strict, ${field.defaultValue}));
break; break;
#elseif ( $field.type == "DOM" ) #elseif ( $field.type == "DOM" )
${classLcapName}.${field.name}( XmlNodeBuilder.build( parser, true ) ); ${classLcapName}.${field.name}(XmlNodeBuilder.build(parser, true));
break; break;
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
List<String> ${field.name} = new ArrayList<>(); List<String> ${field.name} = new ArrayList<>();
while ( parser.nextTag() == XmlPullParser.START_TAG ) while (parser.nextTag() == XmlPullParser.START_TAG) {
{ if ("${Helper.singular($fieldTagName)}".equals(parser.getName())) {
if ( "${Helper.singular($fieldTagName)}".equals( parser.getName() ) ) ${field.name}.add(interpolatedTrimmed(parser.nextText(), "${fieldTagName}"));
{ } else {
${field.name}.add( interpolatedTrimmed( parser.nextText(), "${fieldTagName}" ) ); checkUnknownElement(parser, strict);
}
else
{
checkUnknownElement( parser, strict );
} }
} }
${classLcapName}.${field.name}( ${field.name} ); ${classLcapName}.${field.name}(${field.name});
break; break;
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> ${field.name} = new LinkedHashMap<>(); Map<String, String> ${field.name} = new LinkedHashMap<>();
while ( parser.nextTag() == XmlPullParser.START_TAG ) while (parser.nextTag() == XmlPullParser.START_TAG) {
{
String key = parser.getName(); String key = parser.getName();
String value = parser.nextText().trim(); String value = parser.nextText().trim();
${field.name}.put( key, value ); ${field.name}.put(key, value);
} }
${classLcapName}.${field.name}( ${field.name} ); ${classLcapName}.${field.name}(${field.name});
break; break;
#elseif ( $field.to && $field.multiplicity == "1" ) #elseif ( $field.to && $field.multiplicity == "1" )
${classLcapName}.${field.name}( parse${field.toClass.name}( parser, strict ) ); ${classLcapName}.${field.name}(parse${field.toClass.name}(parser, strict));
break; break;
#elseif ( $field.to && $field.multiplicity == "*" && $Helper.isFlatItems( $field ) ) #elseif ( $field.to && $field.multiplicity == "*" && $Helper.isFlatItems( $field ) )
${field.name}.add( parse${field.toClass.name}( parser, strict ) ); ${field.name}.add(parse${field.toClass.name}(parser, strict));
break; break;
#elseif ( $field.to && $field.multiplicity == "*" ) #elseif ( $field.to && $field.multiplicity == "*" )
List<$field.to> ${field.name} = new ArrayList<>(); List<$field.to> ${field.name} = new ArrayList<>();
while ( parser.nextTag() == XmlPullParser.START_TAG ) while (parser.nextTag() == XmlPullParser.START_TAG) {
{ if ("${Helper.singular($fieldTagName)}".equals(parser.getName())) {
if ( "${Helper.singular($fieldTagName)}".equals( parser.getName() ) ) ${field.name}.add(parse${field.toClass.name}(parser, strict));
{ } else {
${field.name}.add( parse${field.toClass.name}( parser, strict ) ); checkUnknownElement(parser, strict);
}
else
{
checkUnknownElement( parser, strict );
} }
} }
${classLcapName}.${field.name}( ${field.name} ); ${classLcapName}.${field.name}(${field.name});
break; break;
#else #else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
@ -807,20 +690,19 @@ public class ${className}
#set( $ift = "else if" ) #set( $ift = "else if" )
#end #end
#end #end
default: default: {
{ checkUnknownElement(parser, strict);
checkUnknownElement( parser, strict );
break; break;
} }
} }
} }
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#if ( $Helper.isFlatItems( $field ) ) #if ( $Helper.isFlatItems( $field ) )
${classLcapName}.${field.name}( ${field.name} ); ${classLcapName}.${field.name}(${field.name});
#end #end
#end #end
#if ( $class == $root ) #if ( $class == $root )
${classLcapName}.modelEncoding( parser.getInputEncoding() ); ${classLcapName}.modelEncoding(parser.getInputEncoding());
#end #end
return ${classLcapName}.build(); return ${classLcapName}.build();
} }
@ -828,9 +710,7 @@ public class ${className}
#end #end
#end #end
private String checkDuplicate( String tagName, XmlPullParser parser, Set<String> parsed ) private String checkDuplicate(String tagName, XmlPullParser parser, Set<String> parsed) throws XmlPullParserException {
throws XmlPullParserException
{
#set( $aliases = { } ) #set( $aliases = { } )
#set( $flats = { } ) #set( $flats = { } )
#foreach( $class in $model.allClasses ) #foreach( $class in $model.allClasses )
@ -849,8 +729,7 @@ public class ${className}
#end #end
#end #end
#if ( ! ${aliases.isEmpty()} ) #if ( ! ${aliases.isEmpty()} )
switch ( tagName ) switch (tagName) {
{
#foreach( $entry in $aliases.entrySet() ) #foreach( $entry in $aliases.entrySet() )
case "${entry.key}": case "${entry.key}":
tagName = "${entry.value}"; tagName = "${entry.value}";
@ -858,16 +737,14 @@ public class ${className}
} }
#end #end
#if ( ! ${flats.isEmpty()} ) #if ( ! ${flats.isEmpty()} )
switch ( tagName ) switch (tagName) {
{
#foreach( $entry in $flats.entrySet() ) #foreach( $entry in $flats.entrySet() )
case "${entry.key}": case "${entry.key}":
#end #end
break; break;
default: default:
if ( !parsed.add( tagName ) ) if (!parsed.add(tagName)) {
{ throw new XmlPullParserException("Duplicated tag: '" + tagName + "'", parser, null);
throw new XmlPullParserException( "Duplicated tag: '" + tagName + "'", parser, null );
} }
} }
#end #end
@ -879,13 +756,11 @@ public class ${className}
* *
* @param addDefaultEntities a addDefaultEntities object. * @param addDefaultEntities a addDefaultEntities object.
*/ */
public void setAddDefaultEntities( boolean addDefaultEntities ) public void setAddDefaultEntities(boolean addDefaultEntities) {
{
this.addDefaultEntities = addDefaultEntities; this.addDefaultEntities = addDefaultEntities;
} //-- void setAddDefaultEntities( boolean ) } //-- void setAddDefaultEntities(boolean)
public static interface ContentTransformer public static interface ContentTransformer {
{
/** /**
* Interpolate the value read from the xpp3 document * Interpolate the value read from the xpp3 document
* @param source The source value * @param source The source value
@ -893,7 +768,7 @@ public class ${className}
* log stuff. * log stuff.
* @return The interpolated value. * @return The interpolated value.
*/ */
String transform( String source, String fieldName ); String transform(String source, String fieldName);
} }
} }

View File

@ -51,84 +51,74 @@ import ${packageModelV4}.${class.name};
import org.codehaus.plexus.util.xml.Xpp3Dom; import org.codehaus.plexus.util.xml.Xpp3Dom;
@Generated @Generated
public class ${className} public class ${className} {
{
private final Function<String, String> transformer; private final Function<String, String> transformer;
public ${className}( Function<String, String> transformer ) public ${className}(Function<String, String> transformer) {
{
this.transformer = transformer; this.transformer = transformer;
} }
/** /**
* Transforms the given model * Transforms the given model
*/ */
public ${root.name} visit( ${root.name} target ) public ${root.name} visit(${root.name} target) {
{ Objects.requireNonNull(target, "target cannot be null");
Objects.requireNonNull( target, "target cannot be null" ); return transform${root.name}(target);
return transform${root.name}( target );
} }
/** /**
* The transformation function. * The transformation function.
*/ */
protected String transform( String value ) protected String transform(String value) {
{ return transformer.apply(value);
return transformer.apply( value );
} }
#foreach ( $class in $model.allClasses ) #foreach ( $class in $model.allClasses )
#if ( $class.name != "InputSource" && $class.name != "InputLocation" ) #if ( $class.name != "InputSource" && $class.name != "InputLocation" )
#set ( $ancestors = $Helper.ancestors( $class ) ) #set ( $ancestors = $Helper.ancestors( $class ) )
#set ( $allFields = $Helper.xmlFields( $class ) ) #set ( $allFields = $Helper.xmlFields( $class ) )
protected ${class.name} transform${class.name}( ${class.name} target ) protected ${class.name} transform${class.name}(${class.name} target) {
{ if (target == null) {
if ( target == null )
{
return null; return null;
} }
${class.name}.Builder builder = ${class.name}.newBuilder( target ); ${class.name}.Builder builder = ${class.name}.newBuilder(target);
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
transform${field.modelClass.name}_${Helper.capitalise($field.name)}( builder, target ); transform${field.modelClass.name}_${Helper.capitalise($field.name)}(builder, target);
#end #end
return builder.build(); return builder.build();
} }
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#set ( $capField = ${Helper.capitalise($field.name)} ) #set ( $capField = ${Helper.capitalise($field.name)} )
protected void transform${class.name}_${capField}( ${class.name}.Builder builder, ${class.name} target ) protected void transform${class.name}_${capField}(${class.name}.Builder builder, ${class.name} target) {
{
#if ( $field.type == "String" ) #if ( $field.type == "String" )
String newVal = transform( target.get${capField}() ); String newVal = transform(target.get${capField}());
builder.${field.name}( newVal != target.get${capField}() ? newVal : null ); builder.${field.name}(newVal != target.get${capField}() ? newVal : null);
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
builder.${field.name}( transform( target.get${capField}(), this::transform ) ); builder.${field.name}(transform(target.get${capField}(), this::transform));
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
Map<String, String> props = target.get${capField}(); Map<String, String> props = target.get${capField}();
Map<String, String> newProps = null; Map<String, String> newProps = null;
for ( Map.Entry<String, String> entry : props.entrySet() ) for (Map.Entry<String, String> entry : props.entrySet()) {
{ String newVal = transform(entry.getValue());
String newVal = transform( entry.getValue() ); if (newVal != null && newVal != entry.getValue()) {
if ( newVal != null && newVal != entry.getValue() ) if (newProps == null) {
{
if ( newProps == null )
{
newProps = new HashMap<>(); newProps = new HashMap<>();
newProps.putAll( props ); newProps.putAll(props);
builder.${field.name}( newProps ); builder.${field.name}(newProps);
} }
newProps.put( entry.getKey(), newVal ); newProps.put(entry.getKey(), newVal);
} }
} }
#elseif ( $field.to && $field.multiplicity == "1" ) #elseif ( $field.to && $field.multiplicity == "1" )
${field.to} newVal = transform${field.to}( target.get${capField}() ); ${field.to} newVal = transform${field.to}(target.get${capField}());
builder.${field.name}( newVal != target.get${capField}() ? newVal : null ); builder.${field.name}(newVal != target.get${capField}() ? newVal : null);
#elseif ( $field.to && $field.multiplicity == "*" ) #elseif ( $field.to && $field.multiplicity == "*" )
builder.${field.name}( transform( target.get${capField}(), this::transform${field.to} ) ); builder.${field.name}(transform(target.get${capField}(), this::transform${field.to}));
#elseif ( $field.type == "DOM" ) #elseif ( $field.type == "DOM" )
XmlNode newVal = transform( target.get${capField}() ); XmlNode newVal = transform(target.get${capField}());
builder.${field.name}( newVal != target.get${capField}() ? newVal : null ); builder.${field.name}(newVal != target.get${capField}() ? newVal : null);
#elseif ( $field.type == "boolean" || $field.type == "int" || $field.type == "java.nio.file.Path" ) #elseif ( $field.type == "boolean" || $field.type == "int" || $field.type == "java.nio.file.Path" )
// nothing to do, the transformer only handles strings // nothing to do, the transformer only handles strings
#else #else
@ -139,61 +129,49 @@ public class ${className}
#end #end
#end #end
protected <T> List<T> transform( List<T> list, Function<T, T> transformer ) protected <T> List<T> transform(List<T> list, Function<T, T> transformer) {
{
List<T> newList = null; List<T> newList = null;
for ( int i = 0; i < list.size(); i++ ) for (int i = 0; i < list.size(); i++) {
{ T newVal = transformer.apply(list.get(i));
T newVal = transformer.apply( list.get( i ) ); if (newVal != list.get(i)) {
if ( newVal != list.get( i ) ) if (newList == null) {
{ newList = new ArrayList<>(list);
if ( newList == null )
{
newList = new ArrayList<>( list );
} }
newList.set( i, newVal ); newList.set(i, newVal);
} }
} }
return newList; return newList;
} }
protected XmlNode transform( XmlNode node ) protected XmlNode transform(XmlNode node) {
{ if (node != null) {
if ( node != null ) Xpp3Dom xpp = new Xpp3Dom(node);
{ transform(xpp);
Xpp3Dom xpp = new Xpp3Dom( node );
transform( xpp );
return xpp.getDom(); return xpp.getDom();
} }
return node; return node;
} }
protected void transform( Xpp3Dom dom ) protected void transform(Xpp3Dom dom) {
{ if (dom != null) {
if ( dom != null )
{
String org, val; String org, val;
// Content // Content
org = dom.getValue(); org = dom.getValue();
val = transform( org ); val = transform(org);
if ( org != val ) if (org != val) {
{ dom.setValue(val);
dom.setValue( val );
} }
// Attributes // Attributes
for ( String attr : dom.getAttributeNames() ) for (String attr : dom.getAttributeNames()) {
{ org = dom.getAttribute(attr);
org = dom.getAttribute( attr ); val = transform(org);
val = transform( org ); if (org != val) {
if ( org != val ) dom.setAttribute(attr, val);
{
dom.setAttribute( attr, val );
} }
} }
// Children // Children
for ( int i = 0, l = dom.getChildCount(); i < l; i++ ) for (int i = 0, l = dom.getChildCount(); i < l; i++) {
{ transform(dom.getChild(i));
transform( dom.getChild( i ) );
} }
} }
} }

View File

@ -67,8 +67,8 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.codehaus.plexus.util.xml.pull.XmlSerializer; import org.codehaus.plexus.util.xml.pull.XmlSerializer;
@Generated @Generated
public class ${className} public class ${className} {
{
//--------------------------/ //--------------------------/
//- Class/Member Variables -/ //- Class/Member Variables -/
//--------------------------/ //--------------------------/
@ -97,20 +97,18 @@ public class ${className}
* *
* @param fileComment a fileComment object. * @param fileComment a fileComment object.
*/ */
public void setFileComment( String fileComment ) public void setFileComment(String fileComment) {
{
this.fileComment = fileComment; this.fileComment = fileComment;
} //-- void setFileComment( String ) } //-- void setFileComment(String)
/** /**
* Method setStringFormatter. * Method setStringFormatter.
* *
* @param stringFormatter * @param stringFormatter
*/ */
public void setStringFormatter( InputLocation.StringFormatter stringFormatter ) public void setStringFormatter(InputLocation.StringFormatter stringFormatter) {
{
this.stringFormatter = stringFormatter; this.stringFormatter = stringFormatter;
} //-- void setStringFormatter( InputLocation.StringFormatter ) } //-- void setStringFormatter(InputLocation.StringFormatter)
/** /**
* Method write. * Method write.
@ -119,17 +117,15 @@ public class ${className}
* @param ${rootLcapName} a ${root.name} object * @param ${rootLcapName} a ${root.name} object
* @throws java.io.IOException java.io.IOException if any * @throws java.io.IOException java.io.IOException if any
*/ */
public void write( Writer writer, ${root.name} ${rootLcapName} ) public void write(Writer writer, ${root.name} ${rootLcapName}) throws java.io.IOException {
throws java.io.IOException
{
XmlSerializer serializer = new MXSerializer(); XmlSerializer serializer = new MXSerializer();
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " ); serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " ");
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" ); serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n");
serializer.setOutput( writer ); serializer.setOutput(writer);
serializer.startDocument( ${rootLcapName}.getModelEncoding(), null ); serializer.startDocument(${rootLcapName}.getModelEncoding(), null);
write${root.name}( "$rootTag", ${rootLcapName}, serializer ); write${root.name}("$rootTag", ${rootLcapName}, serializer);
serializer.endDocument(); serializer.endDocument();
} //-- void write( Writer, ${root.name} ) } //-- void write(Writer, ${root.name})
/** /**
* Method write. * Method write.
@ -138,17 +134,15 @@ public class ${className}
* @param ${rootLcapName} a ${root.name} object * @param ${rootLcapName} a ${root.name} object
* @throws java.io.IOException java.io.IOException if any * @throws java.io.IOException java.io.IOException if any
*/ */
public void write( OutputStream stream, ${root.name} ${rootLcapName} ) public void write(OutputStream stream, ${root.name} ${rootLcapName}) throws java.io.IOException {
throws java.io.IOException
{
XmlSerializer serializer = new MXSerializer(); XmlSerializer serializer = new MXSerializer();
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " ); serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " ");
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" ); serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n");
serializer.setOutput( stream, ${rootLcapName}.getModelEncoding() ); serializer.setOutput(stream, ${rootLcapName}.getModelEncoding());
serializer.startDocument( ${rootLcapName}.getModelEncoding(), null ); serializer.startDocument(${rootLcapName}.getModelEncoding(), null);
write${root.name}( "$rootTag", ${rootLcapName}, serializer ); write${root.name}("$rootTag", ${rootLcapName}, serializer);
serializer.endDocument(); serializer.endDocument();
} //-- void write( OutputStream, ${root.name} ) } //-- void write(OutputStream, ${root.name})
/** /**
* Method writeDomToSerializer. * Method writeDomToSerializer.
@ -157,29 +151,21 @@ public class ${className}
* @param serializer a serializer object. * @param serializer a serializer object.
* @throws java.io.IOException java.io.IOException if any. * @throws java.io.IOException java.io.IOException if any.
*/ */
protected void writeDomToSerializer( org.apache.maven.api.xml.XmlNode dom, XmlSerializer serializer ) protected void writeDomToSerializer(org.apache.maven.api.xml.XmlNode dom, XmlSerializer serializer) throws java.io.IOException {
throws java.io.IOException serializer.startTag(NAMESPACE, dom.getName());
{ for (Map.Entry<String, String> attribute : dom.getAttributes().entrySet()) {
serializer.startTag( NAMESPACE, dom.getName() ); serializer.attribute(NAMESPACE, attribute.getKey(), attribute.getValue());
for ( Map.Entry<String, String> attribute : dom.getAttributes().entrySet() )
{
serializer.attribute( NAMESPACE, attribute.getKey(), attribute.getValue() );
} }
for ( XmlNode aChild : dom.getChildren() ) for (XmlNode aChild : dom.getChildren()) {
{ writeDomToSerializer(aChild, serializer);
writeDomToSerializer( aChild, serializer );
} }
String value = dom.getValue(); String value = dom.getValue();
if ( value != null ) if (value != null) {
{ serializer.text(value);
serializer.text( value );
} }
serializer.endTag(NAMESPACE, dom.getName());
serializer.endTag( NAMESPACE, dom.getName() ); } //-- void writeDomToSerializer(org.apache.maven.api.xml.XmlNode, XmlSerializer)
} //-- void writeDomToSerializer( org.apache.maven.api.xml.XmlNode, XmlSerializer )
#foreach ( $class in $model.allClasses ) #foreach ( $class in $model.allClasses )
@ -187,35 +173,32 @@ public class ${className}
#set ( $classUcapName = $Helper.capitalise( $class.name ) ) #set ( $classUcapName = $Helper.capitalise( $class.name ) )
#set ( $classLcapName = $Helper.uncapitalise( $class.name ) ) #set ( $classLcapName = $Helper.uncapitalise( $class.name ) )
#set ( $allFields = $Helper.xmlFields( $class ) ) #set ( $allFields = $Helper.xmlFields( $class ) )
private void write${classUcapName}( String tagName, ${classUcapName} ${classLcapName}, XmlSerializer serializer ) private void write${classUcapName}(String tagName, ${classUcapName} ${classLcapName}, XmlSerializer serializer)
throws IOException throws IOException {
{ if (${classLcapName} != null) {
if ( ${classLcapName} != null )
{
#if ( $class == $root ) #if ( $class == $root )
if ( this.fileComment != null ) if (this.fileComment != null) {
{
serializer.comment(this.fileComment); serializer.comment(this.fileComment);
} }
serializer.setPrefix( "", "http://maven.apache.org/POM/4.0.0" ); serializer.setPrefix("", "http://maven.apache.org/POM/4.0.0");
serializer.setPrefix( "xsi", "http://www.w3.org/2001/XMLSchema-instance" ); serializer.setPrefix("xsi", "http://www.w3.org/2001/XMLSchema-instance");
serializer.startTag( NAMESPACE, tagName ); serializer.startTag(NAMESPACE, tagName);
serializer.attribute( "", "xsi:schemaLocation", "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" ); serializer.attribute("", "xsi:schemaLocation", "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd");
#else #else
serializer.startTag( NAMESPACE, tagName ); serializer.startTag(NAMESPACE, tagName);
#end #end
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#if ( $Helper.xmlFieldMetadata( $field ).attribute ) #if ( $Helper.xmlFieldMetadata( $field ).attribute )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName ) #set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#set ( $fieldCapName = $Helper.capitalise( $field.name ) ) #set ( $fieldCapName = $Helper.capitalise( $field.name ) )
#if ( $field.type == "String" ) #if ( $field.type == "String" )
writeAttr( "$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer ); writeAttr("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer);
#elseif ( $field.type == "boolean" ) #elseif ( $field.type == "boolean" )
#set ( $def = ${field.defaultValue} ) #set ( $def = ${field.defaultValue} )
#if ( ${def} == "true" ) #if ( ${def} == "true" )
writeAttr( "$fieldTagName", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer ); writeAttr("$fieldTagName", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer);
#else #else
writeAttr( "$fieldTagName", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer ); writeAttr("$fieldTagName", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer);
#end #end
#else #else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
@ -232,137 +215,112 @@ public class ${className}
#set ( $def = ${field.defaultValue} ) #set ( $def = ${field.defaultValue} )
#if ( $field.type == "String" ) #if ( $field.type == "String" )
#if ( ! $def ) #if ( ! $def )
writeTag( "$fieldTagName", null, ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName} ); writeTag("$fieldTagName", null, ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName});
#else #else
writeTag( "$fieldTagName", "${def}", ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName} ); writeTag("$fieldTagName", "${def}", ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName});
#end #end
#elseif ( $field.type == "boolean" || $field.type == "Boolean" ) #elseif ( $field.type == "boolean" || $field.type == "Boolean" )
#if ( ${def} == "true" ) #if ( ${def} == "true" )
writeTag( "$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer, ${classLcapName} ); writeTag("$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer, ${classLcapName});
#else #else
writeTag( "$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer, ${classLcapName} ); writeTag("$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer, ${classLcapName});
#end #end
#elseif ( $field.type == "int" ) #elseif ( $field.type == "int" )
writeTag( "$fieldTagName", "${def}", Integer.toString( ${classLcapName}.get${fieldCapName}() ), serializer, ${classLcapName} ); writeTag("$fieldTagName", "${def}", Integer.toString(${classLcapName}.get${fieldCapName}()), serializer, ${classLcapName});
#elseif ( $field.type == "DOM" ) #elseif ( $field.type == "DOM" )
writeDom( ${classLcapName}.get${fieldCapName}(), serializer ); writeDom(${classLcapName}.get${fieldCapName}(), serializer);
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
#set( $singularField = ${Helper.singular($fieldTagName)} ) #set( $singularField = ${Helper.singular($fieldTagName)} )
writeList( "$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName}, writeList("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName},
t -> writeTag( "$singularField", null, t, serializer, null ) ); t -> writeTag("$singularField", null, t, serializer, null));
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
writeProperties( "$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName} ); writeProperties("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName});
#elseif ( $field.to && $field.multiplicity == "1" ) #elseif ( $field.to && $field.multiplicity == "1" )
write${field.to}( "$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer ); write${field.to}("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer);
#elseif ( $field.to && $field.multiplicity == "*" ) #elseif ( $field.to && $field.multiplicity == "*" )
#set( $singularField = ${Helper.singular($fieldTagName)} ) #set( $singularField = ${Helper.singular($fieldTagName)} )
writeList( "$fieldTagName", $Helper.isFlatItems($field), ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName}, writeList("$fieldTagName", $Helper.isFlatItems($field), ${classLcapName}.get${fieldCapName}(), serializer, ${classLcapName},
t -> write${field.to}( "$singularField", t, serializer ) ); t -> write${field.to}("$singularField", t, serializer));
#else #else
// TODO: name=${field.name} type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: name=${field.name} type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
#end #end
#end #end
#end #end
serializer.endTag( NAMESPACE, tagName ); serializer.endTag(NAMESPACE, tagName);
writeLocationTracking( ${classLcapName}, "", serializer ); writeLocationTracking(${classLcapName}, "", serializer);
} }
} }
#end #end
#end #end
@FunctionalInterface @FunctionalInterface
private interface ElementWriter<T> private interface ElementWriter<T> {
{ public void write(T t) throws IOException;
public void write( T t ) throws IOException;
} }
private <T> void writeList( String tagName, List<T> list, XmlSerializer serializer, InputLocationTracker locationTracker, ElementWriter<T> writer ) private <T> void writeList(String tagName, List<T> list, XmlSerializer serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException {
throws IOException writeList(tagName, false, list, serializer, locationTracker, writer);
{
writeList( tagName, false, list, serializer, locationTracker, writer );
} }
private <T> void writeList( String tagName, boolean flat, List<T> list, XmlSerializer serializer, InputLocationTracker locationTracker, ElementWriter<T> writer ) private <T> void writeList(String tagName, boolean flat, List<T> list, XmlSerializer serializer, InputLocationTracker locationTracker, ElementWriter<T> writer) throws IOException {
throws IOException if (list != null && !list.isEmpty()) {
{ if (!flat) {
if ( list != null && !list.isEmpty() ) serializer.startTag(NAMESPACE, tagName);
{
if ( !flat )
{
serializer.startTag( NAMESPACE, tagName );
} }
int index = 0; int index = 0;
InputLocation location = locationTracker != null ? locationTracker.getLocation( tagName ) : null; InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null;
for ( T t : list ) for (T t : list) {
{ writer.write(t);
writer.write( t ); writeLocationTracking(location, Integer.valueOf(index++), serializer);
writeLocationTracking( location, Integer.valueOf( index++ ), serializer );
} }
if ( !flat ) if (!flat) {
{ serializer.endTag(NAMESPACE, tagName);
serializer.endTag( NAMESPACE, tagName ); writeLocationTracking(locationTracker, tagName, serializer);
writeLocationTracking( locationTracker, tagName, serializer );
} }
} }
} }
private <T> void writeProperties( String tagName, Map<String, String> props, XmlSerializer serializer, InputLocationTracker locationTracker ) private <T> void writeProperties(String tagName, Map<String, String> props, XmlSerializer serializer, InputLocationTracker locationTracker) throws IOException {
throws IOException if (props != null && !props.isEmpty()) {
{ serializer.startTag(NAMESPACE, tagName);
if ( props != null && !props.isEmpty() ) InputLocation location = locationTracker != null ? locationTracker.getLocation(tagName) : null;
{ for (Map.Entry<String, String> entry : props.entrySet()) {
serializer.startTag( NAMESPACE, tagName );
InputLocation location = locationTracker != null ? locationTracker.getLocation( tagName ) : null;
for ( Map.Entry<String, String> entry : props.entrySet() )
{
String key = entry.getKey(); String key = entry.getKey();
writeTag( key, null, entry.getValue(), serializer, null ); writeTag(key, null, entry.getValue(), serializer, null);
writeLocationTracking( location, key, serializer ); writeLocationTracking(location, key, serializer);
} }
serializer.endTag( NAMESPACE, tagName ); serializer.endTag(NAMESPACE, tagName);
writeLocationTracking( locationTracker, tagName, serializer ); writeLocationTracking(locationTracker, tagName, serializer);
} }
} }
private void writeDom( XmlNode dom, XmlSerializer serializer ) private void writeDom(XmlNode dom, XmlSerializer serializer) throws IOException {
throws IOException if (dom != null) {
{ serializer.startTag(NAMESPACE, dom.getName());
if ( dom != null ) for (Map.Entry<String, String> attr : dom.getAttributes().entrySet()) {
{ serializer.attribute(NAMESPACE, attr.getKey(), attr.getValue());
serializer.startTag( NAMESPACE, dom.getName() );
for ( Map.Entry<String, String> attr : dom.getAttributes().entrySet() )
{
serializer.attribute( NAMESPACE, attr.getKey(), attr.getValue() );
} }
for ( XmlNode child : dom.getChildren() ) for (XmlNode child : dom.getChildren()) {
{ writeDom(child, serializer);
writeDom( child, serializer );
} }
String value = dom.getValue(); String value = dom.getValue();
if ( value != null ) if (value != null) {
{ serializer.text(value);
serializer.text( value );
} }
serializer.endTag( NAMESPACE, dom.getName() ); serializer.endTag(NAMESPACE, dom.getName());
} }
} }
private void writeTag( String tagName, String defaultValue, String value, XmlSerializer serializer, InputLocationTracker locationTracker ) private void writeTag(String tagName, String defaultValue, String value, XmlSerializer serializer, InputLocationTracker locationTracker) throws IOException {
throws IOException if (value != null && !Objects.equals(defaultValue, value)) {
{ serializer.startTag(NAMESPACE, tagName).text(value).endTag(NAMESPACE, tagName);
if ( value != null && !Objects.equals( defaultValue, value ) ) writeLocationTracking(locationTracker, tagName, serializer);
{
serializer.startTag( NAMESPACE, tagName ).text( value ).endTag( NAMESPACE, tagName );
writeLocationTracking( locationTracker, tagName, serializer );
} }
} }
private void writeAttr( String attrName, String value, XmlSerializer serializer ) private void writeAttr(String attrName, String value, XmlSerializer serializer) throws IOException {
throws IOException if (value != null) {
{ serializer.attribute(NAMESPACE, attrName, value);
if ( value != null )
{
serializer.attribute( NAMESPACE, attrName, value );
} }
} }
@ -374,15 +332,12 @@ public class ${className}
* @param key * @param key
* @throws java.io.IOException * @throws java.io.IOException
*/ */
protected void writeLocationTracking( InputLocationTracker locationTracker, Object key, XmlSerializer serializer ) protected void writeLocationTracking(InputLocationTracker locationTracker, Object key, XmlSerializer serializer) throws java.io.IOException {
throws java.io.IOException InputLocation location = (locationTracker == null) ? null : locationTracker.getLocation(key);
{ if (location != null) {
InputLocation location = ( locationTracker == null ) ? null : locationTracker.getLocation( key ); serializer.comment(toString(location));
if ( location != null )
{
serializer.comment( toString( location ) );
} }
} //-- void writeLocationTracking( InputLocationTracker, Object, XmlSerializer ) } //-- void writeLocationTracking(InputLocationTracker, Object, XmlSerializer)
/** /**
* Method toString. * Method toString.
@ -390,12 +345,10 @@ public class ${className}
* @param location * @param location
* @return String * @return String
*/ */
protected String toString( InputLocation location ) protected String toString(InputLocation location) {
{ if (stringFormatter != null) {
if ( stringFormatter != null ) return stringFormatter.toString(location);
{
return stringFormatter.toString( location );
} }
return ' ' + location.getSource().toString() + ':' + location.getLineNumber() + ' '; return ' ' + location.getSource().toString() + ':' + location.getLineNumber() + ' ';
} //-- String toString( InputLocation ) } //-- String toString(InputLocation)
} }

View File

@ -63,8 +63,8 @@ import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
import org.codehaus.plexus.util.xml.pull.XmlSerializer; import org.codehaus.plexus.util.xml.pull.XmlSerializer;
@Generated @Generated
public class ${className} public class ${className} {
{
//--------------------------/ //--------------------------/
//- Class/Member Variables -/ //- Class/Member Variables -/
//--------------------------/ //--------------------------/
@ -89,10 +89,9 @@ public class ${className}
* *
* @param fileComment a fileComment object. * @param fileComment a fileComment object.
*/ */
public void setFileComment( String fileComment ) public void setFileComment(String fileComment) {
{
this.fileComment = fileComment; this.fileComment = fileComment;
} //-- void setFileComment( String ) } //-- void setFileComment(String)
/** /**
* Method write. * Method write.
@ -101,17 +100,15 @@ public class ${className}
* @param ${rootLcapName} a ${root.name} object * @param ${rootLcapName} a ${root.name} object
* @throws java.io.IOException java.io.IOException if any * @throws java.io.IOException java.io.IOException if any
*/ */
public void write( Writer writer, ${root.name} ${rootLcapName} ) public void write(Writer writer, ${root.name} ${rootLcapName}) throws java.io.IOException {
throws java.io.IOException
{
XmlSerializer serializer = new MXSerializer(); XmlSerializer serializer = new MXSerializer();
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " ); serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " ");
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" ); serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n");
serializer.setOutput( writer ); serializer.setOutput(writer);
serializer.startDocument( ${rootLcapName}.getModelEncoding(), null ); serializer.startDocument(${rootLcapName}.getModelEncoding(), null);
write${root.name}( "$rootTag", ${rootLcapName}, serializer ); write${root.name}("$rootTag", ${rootLcapName}, serializer);
serializer.endDocument(); serializer.endDocument();
} //-- void write( Writer, ${root.name} ) } //-- void write(Writer, ${root.name})
/** /**
* Method write. * Method write.
@ -120,17 +117,15 @@ public class ${className}
* @param ${rootLcapName} a ${root.name} object * @param ${rootLcapName} a ${root.name} object
* @throws java.io.IOException java.io.IOException if any * @throws java.io.IOException java.io.IOException if any
*/ */
public void write( OutputStream stream, ${root.name} ${rootLcapName} ) public void write(OutputStream stream, ${root.name} ${rootLcapName}) throws java.io.IOException {
throws java.io.IOException
{
XmlSerializer serializer = new MXSerializer(); XmlSerializer serializer = new MXSerializer();
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " " ); serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-indentation", " ");
serializer.setProperty( "http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n" ); serializer.setProperty("http://xmlpull.org/v1/doc/properties.html#serializer-line-separator", "\n");
serializer.setOutput( stream, ${rootLcapName}.getModelEncoding() ); serializer.setOutput(stream, ${rootLcapName}.getModelEncoding());
serializer.startDocument( ${rootLcapName}.getModelEncoding(), null ); serializer.startDocument(${rootLcapName}.getModelEncoding(), null);
write${root.name}( "$rootTag", ${rootLcapName}, serializer ); write${root.name}("$rootTag", ${rootLcapName}, serializer);
serializer.endDocument(); serializer.endDocument();
} //-- void write( OutputStream, ${root.name} ) } //-- void write(OutputStream, ${root.name})
/** /**
* Method writeDomToSerializer. * Method writeDomToSerializer.
@ -139,29 +134,21 @@ public class ${className}
* @param serializer a serializer object. * @param serializer a serializer object.
* @throws java.io.IOException java.io.IOException if any. * @throws java.io.IOException java.io.IOException if any.
*/ */
protected void writeDomToSerializer( org.apache.maven.api.xml.XmlNode dom, XmlSerializer serializer ) protected void writeDomToSerializer(org.apache.maven.api.xml.XmlNode dom, XmlSerializer serializer) throws java.io.IOException {
throws java.io.IOException serializer.startTag(NAMESPACE, dom.getName());
{ for (Map.Entry<String, String> attribute : dom.getAttributes().entrySet()) {
serializer.startTag( NAMESPACE, dom.getName() ); serializer.attribute(NAMESPACE, attribute.getKey(), attribute.getValue());
for ( Map.Entry<String, String> attribute : dom.getAttributes().entrySet() )
{
serializer.attribute( NAMESPACE, attribute.getKey(), attribute.getValue() );
} }
for ( XmlNode aChild : dom.getChildren() ) for (XmlNode aChild : dom.getChildren()) {
{ writeDomToSerializer(aChild, serializer);
writeDomToSerializer( aChild, serializer );
} }
String value = dom.getValue(); String value = dom.getValue();
if ( value != null ) if (value != null) {
{ serializer.text(value);
serializer.text( value );
} }
serializer.endTag(NAMESPACE, dom.getName());
serializer.endTag( NAMESPACE, dom.getName() ); } //-- void writeDomToSerializer(org.apache.maven.api.xml.XmlNode, XmlSerializer)
} //-- void writeDomToSerializer( org.apache.maven.api.xml.XmlNode, XmlSerializer )
#foreach ( $class in $model.allClasses ) #foreach ( $class in $model.allClasses )
@ -169,35 +156,31 @@ public class ${className}
#set ( $classUcapName = $Helper.capitalise( $class.name ) ) #set ( $classUcapName = $Helper.capitalise( $class.name ) )
#set ( $classLcapName = $Helper.uncapitalise( $class.name ) ) #set ( $classLcapName = $Helper.uncapitalise( $class.name ) )
#set ( $allFields = $Helper.xmlFields( $class ) ) #set ( $allFields = $Helper.xmlFields( $class ) )
private void write${classUcapName}( String tagName, ${classUcapName} ${classLcapName}, XmlSerializer serializer ) private void write${classUcapName}(String tagName, ${classUcapName} ${classLcapName}, XmlSerializer serializer) throws IOException {
throws IOException if (${classLcapName} != null) {
{
if ( ${classLcapName} != null )
{
#if ( $class == $root ) #if ( $class == $root )
if ( this.fileComment != null ) if (this.fileComment != null) {
{
serializer.comment(this.fileComment); serializer.comment(this.fileComment);
} }
serializer.setPrefix( "", "http://maven.apache.org/POM/4.0.0" ); serializer.setPrefix("", "http://maven.apache.org/POM/4.0.0");
serializer.setPrefix( "xsi", "http://www.w3.org/2001/XMLSchema-instance" ); serializer.setPrefix("xsi", "http://www.w3.org/2001/XMLSchema-instance");
serializer.startTag( NAMESPACE, tagName ); serializer.startTag(NAMESPACE, tagName);
serializer.attribute( "", "xsi:schemaLocation", "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" ); serializer.attribute("", "xsi:schemaLocation", "http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd");
#else #else
serializer.startTag( NAMESPACE, tagName ); serializer.startTag(NAMESPACE, tagName);
#end #end
#foreach ( $field in $allFields ) #foreach ( $field in $allFields )
#if ( $Helper.xmlFieldMetadata( $field ).attribute ) #if ( $Helper.xmlFieldMetadata( $field ).attribute )
#set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName ) #set ( $fieldTagName = $Helper.xmlFieldMetadata( $field ).tagName )
#set ( $fieldCapName = $Helper.capitalise( $field.name ) ) #set ( $fieldCapName = $Helper.capitalise( $field.name ) )
#if ( $field.type == "String" ) #if ( $field.type == "String" )
writeAttr( "$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer ); writeAttr("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer);
#elseif ( $field.type == "boolean" ) #elseif ( $field.type == "boolean" )
#set ( $def = ${field.defaultValue} ) #set ( $def = ${field.defaultValue} )
#if ( ${def} == "true" ) #if ( ${def} == "true" )
writeAttr( "$fieldTagName", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer ); writeAttr("$fieldTagName", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer);
#else #else
writeAttr( "$fieldTagName", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer ); writeAttr("$fieldTagName", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer);
#end #end
#else #else
// TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
@ -214,125 +197,100 @@ public class ${className}
#set ( $def = ${field.defaultValue} ) #set ( $def = ${field.defaultValue} )
#if ( $field.type == "String" ) #if ( $field.type == "String" )
#if ( ! $def ) #if ( ! $def )
writeTag( "$fieldTagName", null, ${classLcapName}.get${fieldCapName}(), serializer ); writeTag("$fieldTagName", null, ${classLcapName}.get${fieldCapName}(), serializer);
#else #else
writeTag( "$fieldTagName", "${def}", ${classLcapName}.get${fieldCapName}(), serializer ); writeTag("$fieldTagName", "${def}", ${classLcapName}.get${fieldCapName}(), serializer);
#end #end
#elseif ( $field.type == "boolean" || $field.type == "Boolean" ) #elseif ( $field.type == "boolean" || $field.type == "Boolean" )
#if ( ${def} == "true" ) #if ( ${def} == "true" )
writeTag( "$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer ); writeTag("$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? null : "false", serializer);
#else #else
writeTag( "$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer ); writeTag("$fieldTagName", "${def}", ${classLcapName}.is${fieldCapName}() ? "true" : null, serializer);
#end #end
#elseif ( $field.type == "int" ) #elseif ( $field.type == "int" )
writeTag( "$fieldTagName", "${def}", Integer.toString( ${classLcapName}.get${fieldCapName}() ), serializer ); writeTag("$fieldTagName", "${def}", Integer.toString(${classLcapName}.get${fieldCapName}()), serializer);
#elseif ( $field.type == "DOM" ) #elseif ( $field.type == "DOM" )
writeDom( ${classLcapName}.get${fieldCapName}(), serializer ); writeDom(${classLcapName}.get${fieldCapName}(), serializer);
#elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.List" && $field.to == "String" && $field.multiplicity == "*" )
#set( $singularField = ${Helper.singular($fieldTagName)} ) #set( $singularField = ${Helper.singular($fieldTagName)} )
writeList( "$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer, t -> writeTag( "$singularField", "${def}", t, serializer ) ); writeList("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer, t -> writeTag("$singularField", "${def}", t, serializer));
#elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" ) #elseif ( $field.type == "java.util.Properties" && $field.to == "String" && $field.multiplicity == "*" )
writeProperties( "$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer ); writeProperties("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer);
#elseif ( $field.to && $field.multiplicity == "1" ) #elseif ( $field.to && $field.multiplicity == "1" )
write${field.to}( "$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer ); write${field.to}("$fieldTagName", ${classLcapName}.get${fieldCapName}(), serializer);
#elseif ( $field.to && $field.multiplicity == "*" ) #elseif ( $field.to && $field.multiplicity == "*" )
#set( $singularField = ${Helper.singular($fieldTagName)} ) #set( $singularField = ${Helper.singular($fieldTagName)} )
writeList( "$fieldTagName", $Helper.isFlatItems($field), ${classLcapName}.get${fieldCapName}(), serializer, t -> write${field.to}( "$singularField", t, serializer ) ); writeList("$fieldTagName", $Helper.isFlatItems($field), ${classLcapName}.get${fieldCapName}(), serializer, t -> write${field.to}("$singularField", t, serializer));
#else #else
// TODO: name=${field.name} type=${field.type} to=${field.to} multiplicity=${field.multiplicity} // TODO: name=${field.name} type=${field.type} to=${field.to} multiplicity=${field.multiplicity}
#end #end
#end #end
#end #end
serializer.endTag( NAMESPACE, tagName ); serializer.endTag(NAMESPACE, tagName);
} }
} }
#end #end
#end #end
@FunctionalInterface @FunctionalInterface
private interface ElementWriter<T> private interface ElementWriter<T> {
{ public void write(T t) throws IOException;
public void write( T t ) throws IOException;
} }
private <T> void writeList( String tagName, List<T> list, XmlSerializer serializer, ElementWriter<T> writer ) private <T> void writeList(String tagName, List<T> list, XmlSerializer serializer, ElementWriter<T> writer) throws IOException {
throws IOException writeList(tagName, false, list, serializer, writer);
{
writeList( tagName, false, list, serializer, writer );
} }
private <T> void writeList( String tagName, boolean flat, List<T> list, XmlSerializer serializer, ElementWriter<T> writer ) private <T> void writeList(String tagName, boolean flat, List<T> list, XmlSerializer serializer, ElementWriter<T> writer) throws IOException {
throws IOException if (list != null && !list.isEmpty()) {
{ if (!flat) {
if ( list != null && !list.isEmpty() ) serializer.startTag(NAMESPACE, tagName);
{
if ( !flat )
{
serializer.startTag( NAMESPACE, tagName );
} }
for ( T t : list ) for (T t : list) {
{ writer.write(t);
writer.write( t );
} }
if ( !flat ) if (!flat) {
{ serializer.endTag(NAMESPACE, tagName);
serializer.endTag( NAMESPACE, tagName );
} }
} }
} }
private <T> void writeProperties( String tagName, Map<String, String> props, XmlSerializer serializer ) private <T> void writeProperties(String tagName, Map<String, String> props, XmlSerializer serializer) throws IOException {
throws IOException if (props != null && !props.isEmpty()) {
{ serializer.startTag(NAMESPACE, tagName);
if ( props != null && !props.isEmpty() ) for (Map.Entry<String, String> entry : props.entrySet()) {
{ writeTag(entry.getKey(), null, entry.getValue(), serializer);
serializer.startTag( NAMESPACE, tagName );
for ( Map.Entry<String, String> entry : props.entrySet() )
{
writeTag( entry.getKey(), null, entry.getValue(), serializer );
} }
serializer.endTag( NAMESPACE, tagName ); serializer.endTag(NAMESPACE, tagName);
} }
} }
private void writeDom( XmlNode dom, XmlSerializer serializer ) private void writeDom(XmlNode dom, XmlSerializer serializer) throws IOException {
throws IOException if (dom != null) {
{ serializer.startTag(NAMESPACE, dom.getName());
if ( dom != null ) for (Map.Entry<String, String> attr : dom.getAttributes().entrySet()) {
{ serializer.attribute(NAMESPACE, attr.getKey(), attr.getValue());
serializer.startTag( NAMESPACE, dom.getName() );
for ( Map.Entry<String, String> attr : dom.getAttributes().entrySet() )
{
serializer.attribute( NAMESPACE, attr.getKey(), attr.getValue() );
} }
for ( XmlNode child : dom.getChildren() ) for (XmlNode child : dom.getChildren()) {
{ writeDom(child, serializer);
writeDom( child, serializer );
} }
String value = dom.getValue(); String value = dom.getValue();
if ( value != null ) if (value != null) {
{ serializer.text(value);
serializer.text( value );
} }
serializer.endTag( NAMESPACE, dom.getName() ); serializer.endTag(NAMESPACE, dom.getName());
} }
} }
private void writeTag( String tagName, String defaultValue, String value, XmlSerializer serializer ) private void writeTag(String tagName, String defaultValue, String value, XmlSerializer serializer) throws IOException {
throws IOException if (value != null && !Objects.equals(defaultValue, value)) {
{ serializer.startTag(NAMESPACE, tagName).text(value).endTag(NAMESPACE, tagName);
if ( value != null && !Objects.equals( defaultValue, value ) )
{
serializer.startTag( NAMESPACE, tagName ).text( value ).endTag( NAMESPACE, tagName );
} }
} }
private void writeAttr( String attrName, String value, XmlSerializer serializer ) private void writeAttr(String attrName, String value, XmlSerializer serializer) throws IOException {
throws IOException if (value != null) {
{ serializer.attribute(NAMESPACE, attrName, value);
if ( value != null )
{
serializer.attribute( NAMESPACE, attrName, value );
} }
} }