javolution.xml.XMLContext Maven / Gradle / Ivy
Show all versions of javolution-core-java-msftbx Show documentation
/*
* 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;
import javolution.context.AbstractContext;
import javolution.context.FormatContext;
import javolution.osgi.internal.OSGiServices;
import javolution.text.TextFormat;
/**
* A context for XML parsing/formatting. This context provides
* the {@link javolution.xml.XMLFormat XMLFormat} to parse/format objects
* of any class. If not superseded, the XML format for a class is specified
* by the {@link javolution.xml.DefaultXMLFormat DefaultXMLFormat}
* annotation.
* A XML context always returns the most specialized format. If a class
* has no default format annotation (inherited or not), then the default
* {@link java.lang.Object} format (with "value" attribute is
* parsed/formatted using {@link javolution.text.TextContext current
* text format}) is returned.
* A predefined format exists for the following standard types:
*
* - java.lang.Object (attribute "value" parsed/formatted using {@link TextFormat})
* - java.util.Collection
* - java.util.Map
*
*
*
* @author Jean-Marie Dautelle
* @version 6.0 December 12, 2012
*/
public abstract class XMLContext extends FormatContext {
/**
* Default constructor.
*/
protected XMLContext() {}
/**
* Enters and returns a new xml context instance.
*/
public static XMLContext enter() {
return (XMLContext) XMLContext.currentXMLContext().enterInner();
}
/**
* Returns the xml format for the specified type; if none defined
* the default object xml format (based on {@link TextFormat}) is returned.
*/
public static XMLFormat getFormat(Class extends T> type) {
return XMLContext.currentXMLContext().searchFormat(type);
}
/**
* Sets the xml format for the specified type (and its sub-types).
*/
public abstract void setFormat(Class extends T> type,
XMLFormat format);
/**
* Searches the xml format for the specified type.
*/
protected abstract XMLFormat searchFormat(
Class extends T> type);
/**
* Returns the current xml context.
*/
private static XMLContext currentXMLContext() {
XMLContext ctx = AbstractContext.current(XMLContext.class);
if (ctx != null)
return ctx;
return OSGiServices.getXMLContext();
}
}