ucar.util.prefs.Bean Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package ucar.util.prefs;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.*;
class Bean {
private Object o; // the wrapped object
private BeanParser p = null; // the bean parser (shared for all beans of same class)
// wrap an object in a Bean
public Bean(Object o) {
this.o = o;
}
// create a bean from an XML element
public Bean(org.xml.sax.Attributes atts) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
String className = atts.getValue("class");
Class> c = Class.forName(className);
o = c.newInstance();
p = BeanParser.getParser( c);
p.readProperties(o, atts);
}
// write XML using the bean properties of the contained object
public void writeProperties(PrintWriter out) throws IOException {
if (p == null) p = BeanParser.getParser( o.getClass());
p.writeProperties(o, out);
}
// get the wrapped object
public Object getObject() { return o; }
public Class> getBeanClass() { return o.getClass(); }
static class Collection {
private java.util.Collection