Some Random and Important technical stuff
Some Random and Important technical stuff
public class DefaultedMap<K,V> extends AbstractMapDecorator<K,V> implements Serializable
Decorates another
public Object parseXmlFromFile(Class messageClass, String xmlFile) throws JAXBException, FileNotFoundException {
return parse(messageClass, getXMLInputSourceFromFile(xmlFile));
}
protected Object parse(Class messageClass, InputSource xmlSource) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(messageClass.getPackage().getName());
Unmarshaller unmarshaller = context.createUnmarshaller();
Object message = unmarshaller.unmarshal(xmlSource);
return message;
}
protected static InputSource getXMLInputSourceFromClasspath(String xmlFile) {
InputSource source = new InputSource(AbstractConfigResources.class.getResourceAsStream(xmlFile));
return source;
}
protected static InputSource getXMLInputSourceFromFile(String xmlFile) throws FileNotFoundException {
InputSource source = new InputSource(new FileInputStream(xmlFile));
return source;
}
Map
returning a default value if the map does not contain the requested key.
When the
get(Object)
method is called with a key that does not exist in the map, this map will return the default value specified in the constructor/factory. Only the get method is altered, so the Map.containsKey(Object)
can be used to determine if a key really is in the map or not.
The defaulted value is not added to the map. Compare this behaviour with
LazyMap
, which does add the value to the map (via a Transformer).
For instance:
Map map = new DefaultedMap("NULL"); Object obj = map.get("Surname"); // obj == "NULL"After the above code is executed the map is still empty.
Note that DefaultedMap is not synchronized and is not thread-safe. If you wish to use this map from multiple threads concurrently, you must use appropriate synchronization. The simplest approach is to wrap this map using
Collections.synchronizedMap(Map)
. This class may throw exceptions when accessed by concurrent threads without synchronization.About XML and XSD, Parsing, Marshalling and Unmarshalling of XML and XSD
- We should follow the sequence of elements of XSD while creating an xml file.
- How to parse, Unmarshall xml file? Sample code follows
return parse(messageClass, getXMLInputSourceFromFile(xmlFile));
}
protected Object parse(Class messageClass, InputSource xmlSource) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(messageClass.getPackage().getName());
Unmarshaller unmarshaller = context.createUnmarshaller();
Object message = unmarshaller.unmarshal(xmlSource);
return message;
}
protected static InputSource getXMLInputSourceFromClasspath(String xmlFile) {
InputSource source = new InputSource(AbstractConfigResources.class.getResourceAsStream(xmlFile));
return source;
}
protected static InputSource getXMLInputSourceFromFile(String xmlFile) throws FileNotFoundException {
InputSource source = new InputSource(new FileInputStream(xmlFile));
return source;
}
Comments
Post a Comment