Created
March 28, 2016 12:03
-
-
Save AlexGabor/38cdb6841333aa681244 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ro.oneandonly.bookstore.util; | |
import org.jdom2.Document; | |
import org.jdom2.Element; | |
import org.jdom2.JDOMException; | |
import org.jdom2.input.SAXBuilder; | |
import org.jdom2.output.Format; | |
import org.jdom2.output.XMLOutputter; | |
import ro.oneandonly.bookstore.domain.BaseEntity; | |
import ro.oneandonly.bookstore.domain.validators.BookStoreException; | |
import java.io.FileWriter; | |
import java.io.IOException; | |
import java.lang.reflect.Field; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.Objects; | |
import java.util.stream.Collectors; | |
public class XmlParser<T> { | |
private String filename; | |
public XmlParser(String fileName) { | |
this.filename = fileName; | |
} | |
public void save(T entity) throws JDOMException, IOException, IllegalAccessException { | |
SAXBuilder saxBuilder = new SAXBuilder(); | |
Document document = saxBuilder.build(filename); | |
Element rootElement = document.getRootElement(); | |
serializeField(rootElement, entity); | |
XMLOutputter xmlOutput = new XMLOutputter(); | |
xmlOutput.setFormat(Format.getPrettyFormat()); | |
xmlOutput.output(document, new FileWriter(filename)); | |
} | |
private void serializeField(Element rootElement, Object entity) throws IllegalAccessException { | |
Element entityElement = new Element(entity.getClass().getSimpleName()); | |
entityElement.setAttribute("type", entity.getClass().getTypeName()); | |
rootElement.addContent(entityElement); | |
Field[] declaredFields = entity.getClass().getDeclaredFields(); | |
for (Field field : declaredFields) { | |
field.setAccessible(true); | |
Element element = new Element(field.getName()); | |
if (Collection.class.isAssignableFrom(field.getType())) { | |
element.setAttribute("type", field.getType().getName()); | |
Collection<?> collection = (Collection<?>) field.get(entity); | |
collection.forEach(e -> { | |
try { | |
serializeField(element, field.get(entity)); | |
} catch (IllegalAccessException exception) { | |
throw new BookStoreException(exception.getMessage(), exception); | |
} | |
}); | |
} else { | |
element.setAttribute("type", field.getType().getName()); | |
element.setText(field.get(entity).toString()); | |
} | |
entityElement.addContent(element); | |
} | |
} | |
public List<T> loadEntities() throws JDOMException, IOException { | |
SAXBuilder saxBuilder = new SAXBuilder(); | |
Document document = saxBuilder.build(filename); | |
Element rootElement = document.getRootElement(); | |
return rootElement.getChildren().stream().map(this::<T>deserializeField).collect(Collectors.toList()); | |
} | |
private <E> E deserializeField(Element element) { | |
String type = element.getAttributeValue("type"); | |
E entity; | |
Class entityClass; | |
try { | |
entityClass = Class.forName(type); | |
entity = (E) entityClass.newInstance(); | |
} catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) { | |
throw new BookStoreException(e.getMessage(), e); | |
} | |
if (Collection.class.isAssignableFrom(entityClass)) { | |
element.getChildren().stream().map(this::) | |
} | |
Field[] fields = entity.getClass().getDeclaredFields(); | |
for (Field field: fields) { | |
field.setAccessible(true); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment