javolution.xml.internal.XMLContextImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of javolution-core-java-msftbx
Show all versions of javolution-core-java-msftbx
Only the Java Core part of Javolution library, with slight modifications for use in MSFTBX.
The newest version!
/*
* Javolution - Java(TM) Solution for Real-Time and Embedded Systems
* Copyright (C) 2012 - Javolution (http://javolution.org/)
* All rights reserved.
*
* Permission to use, copy, modify, and distribute this software is
* freely granted, provided that this notice is preserved.
*/
package javolution.xml.internal;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javolution.context.LogContext;
import javolution.text.CharArray;
import javolution.text.TextBuilder;
import javolution.text.TextContext;
import javolution.text.TextFormat;
import javolution.util.FastMap;
import javolution.xml.DefaultXMLFormat;
import javolution.xml.XMLContext;
import javolution.xml.XMLFormat;
import javolution.xml.stream.XMLStreamException;
/**
* Holds the default implementation of XMLContext.
*
* @author Jean-Marie Dautelle
* @version 6.0, July 21, 2013
*/
@SuppressWarnings("rawtypes")
public final class XMLContextImpl extends XMLContext {
// Holds class->format mapping.
private final FastMap, XMLFormat>> classToFormat = new FastMap, XMLFormat>>()
.shared();
// Holds parent (null if root).
private final XMLContextImpl parent;
/** Default constructor for root */
public XMLContextImpl() {
parent = null;
}
/** Inner constructor */
public XMLContextImpl(XMLContextImpl parent) {
this.parent = parent;
}
@Override
protected XMLContext inner() {
return new XMLContextImpl(this);
}
@SuppressWarnings("unchecked")
@Override
protected XMLFormat searchFormat(Class extends T> type) {
XMLFormat format = (XMLFormat) classToFormat.get(type);
if (format != null) return format;
if (parent != null) { // Searches parent.
format = parent.searchFormat(type);
classToFormat.put(type, format);
return format;
}
// Root context (search inheritable annotations).
DefaultXMLFormat annotation = type
.getAnnotation(DefaultXMLFormat.class);
if (annotation != null) { // Found it.
try {
format = (XMLFormat) annotation.value().newInstance();
classToFormat.put(type, format);
return format;
} catch (Throwable error) {
LogContext.warning(error);
}
}
if (Collection.class.isAssignableFrom(type)) {
classToFormat.put(type, COLLECTION_XML);
return (XMLFormat) COLLECTION_XML;
} else if (Map.class.isAssignableFrom(type)) {
classToFormat.put(type, MAP_XML);
return (XMLFormat) MAP_XML;
} else {
classToFormat.put(type, OBJECT_XML);
return (XMLFormat) OBJECT_XML;
}
}
@Override
public void setFormat(Class extends T> type, XMLFormat format) {
classToFormat.put(type, format);
}
/////////////////////////
// PREDEFINED FORMATS //
/////////////////////////
/**
* THe default Object XML representation consists of the text representation
* of the object as a "value" attribute.
*/
private static final XMLFormat