All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.konig.maven.project.generator.XmlSerializer Maven / Gradle / Ivy

The newest version!
package io.konig.maven.project.generator;

/*
 * #%L
 * Konig Maven Project Generator
 * %%
 * Copyright (C) 2015 - 2017 Gregory McFall
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import java.io.File;
import java.io.PrintWriter;
import java.io.Writer;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class XmlSerializer {
	
	private int indentWidth = 3;

	private PrintWriter out;
	private int indent;
	
	public XmlSerializer(Writer out) {
		this.out = out instanceof PrintWriter ? (PrintWriter) out : new PrintWriter(out);
		indent = 0;
	}
	
	public void flush() {
		out.flush();
	}
	
	public int getIndentWidth() {
		return indentWidth;
	}


	public void setIndentWidth(int indentWidth) {
		this.indentWidth = indentWidth;
	}


	public int getIndent() {
		return indent;
	}



	public void setIndent(int indent) {
		this.indent = indent;
	}



	public void write(Object pojo, String tag) {
		beginTag(tag);
		out.println();
		
		if (pojo.getClass().isArray()) {
			Class componentType = pojo.getClass().getComponentType();
			if (componentType == String.class) {
				printArray((String[]) pojo);
			} else {
				printArray((Object[]) pojo);
			}
		} else if (pojo instanceof Collection) {
			printCollection((Collection) pojo);
		} else {
			printProperties(pojo);
		}
		indent();
		endTag(tag);
		
	}


	

	private void printCollection(Collection container) {
		push();
		for (Object e : container) {
			Class type = e.getClass();
			indent();
			if (type == String.class) {
				printSimpleValue(e, "param");
			} else {
				String tag = tagName(type.getSimpleName());
				write(e, tag);
			}
		}
		pop();
		
	}

	private String tagName(String simpleName) {
		StringBuilder builder = new StringBuilder();
		builder.append(Character.toLowerCase(simpleName.charAt(0)));
		for (int i=1; i fieldList = new ArrayList<>();
		addFields(pojo.getClass(), fieldList);
		for (Field field : fieldList) {
			field.setAccessible(true);
			try {
				Object value = field.get(pojo);
				if (value != null) {
					indent();
					String fieldName = field.getName();
					if (isSimpleValue(value)) {
						printSimpleValue(value, fieldName);
					} else if (isEnumValue(value)) {
						printSimpleValue(enumName(value), fieldName);
					} else if (value instanceof Map) {
						// Ignore map fields.
					} else {
						write(value, fieldName);
					}
					
				}
			} catch (Throwable e) {
				throw new RuntimeException(e);
			}
		}
		pop();
		
	}

	private Object enumName(Object value) {
		Enum enumValue = (Enum) value;
		return enumValue.name();
	}

	private boolean isEnumValue(Object value) {
		return value.getClass().isEnum();
	}

	private void printSimpleValue(Object value, String fieldName) {

		beginTag(fieldName);
		String text = value.toString();
		if (value instanceof File) {
			text = text.replace('\\', '/');
		}
		out.print(text);
		endTag(fieldName);
		
	}

	private boolean isSimpleValue(Object value) {
		
		return 
			(value instanceof String) ||
			(value instanceof File) ||
			(value instanceof Number) ||
			(value.getClass() == Boolean.class);
	}



	private void beginTag(String tag) {
		out.print('<');
		out.print(tag);
		out.print('>');
		
	}
	
	private void endTag(String tag) {
		out.print("');
		
	}


	public void indent() {
		for (int i=0; i type, List fieldList) {
		
		Field[] array = type.getDeclaredFields();
		for (Field field : array) {
			if (!Modifier.isStatic(field.getModifiers())) {
				fieldList.add(field);
			}
		}
		
		Class superClass = type.getSuperclass();
		if (superClass != Object.class && superClass!=null) {
			addFields(superClass, fieldList);
		}
		
	}

	private void push() {
		indent++;
	}
	
	private void pop() {
		indent--;
	}
	private void printArray(String[] array){

		push();
		for (Object value : array) {
			indent();
			out.print("");
			out.print(value);
			out.println("");
		}
		pop();

	}
	
	private void printArray(Object[] array){

		String tagName = tagName(array.getClass().getComponentType().getSimpleName());
		push();
		for (Object pojo : array) {
			indent();
			write(pojo, tagName);
		}
		pop();

	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy