|
|
import java.io.IOException;import javax.xml.bind.JAXBException;import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlAccessorType(XmlAccessType.NONE)@XmlRootElement(name = "jmeld")public class JMeldSettings {// class variables:public static JMeldSettings instance;// Instance variables:@XmlElement(name = "editor")private String editor = "editor";@XmlElement(name = "filter")private String filter = "filter";@XmlElement(name = "folder")private String folder = "folder";public JMeldSettings() {}public static synchronized JMeldSettings getInstance() {if(instance == null) {synchronized(JMeldSettings.class) {if(instance == null) {instance = new JMeldSettings();}}}return instance;}/** * Save a configuration to a file. */public void save(JMeldSettings configuration) throws JAXBException, IOException {JaxbPersister.getInstance().save(configuration);}public static void main(String[] args) {try {getInstance().save(getInstance());} catch(JAXBException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch(IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import java.util.HashMap;import java.util.Map;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import javax.xml.bind.Unmarshaller;public class JaxbPersister {// class variables:private static JaxbPersister instance = new JaxbPersister();// instance variables:private Map<Class, Context> contexts = new HashMap<Class, Context>();private JaxbPersister() {}public static JaxbPersister getInstance() {return instance;}/** * Load a object of type 'clazz' from a file. */public <T> T load(Class<T> clazz, File file) throws FileNotFoundException, JAXBException {return load(clazz, new FileInputStream(file));}/** * Load a object of type 'clazz' from a file. */public <T> T load(Class<T> clazz, InputStream is) throws JAXBException {Context context;T object;context = getContext(clazz);synchronized(context) {object = (T) context.unmarshal(is);return object;}}/** * Save an object to a file. */public void save(Object object) throws JAXBException, IOException {OutputStream os; File file = new File("JMeldSettings.xml");os = new FileOutputStream(file);save(object, os);os.close();}/** * Save a object to a outputstream. */private void save(Object object, OutputStream os) throws JAXBException, IOException {Writer writer;Context context;writer = new OutputStreamWriter(os);context = getContext(object.getClass());synchronized(context) {context.marshal(object, writer);}}/** * Each class has it's own context to marshal and unmarshal. The context * contains a jaxbcontext. */private Context getContext(Class clazz) {Context c;synchronized(contexts) {c = contexts.get(clazz);if(c == null) {c = new Context(clazz);contexts.put(clazz, c);}}return c;}class Context {private JAXBContext jaxbContext;private Marshaller marshaller;private Unmarshaller unmarshaller;Context(Class clazz) {try {jaxbContext = JAXBContext.newInstance(clazz);marshaller = jaxbContext.createMarshaller();marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);marshaller.setSchema(null);unmarshaller = jaxbContext.createUnmarshaller();unmarshaller.setSchema(null);} catch(JAXBException e) {e.printStackTrace();}}public void marshal(Object object, Writer writer) throws JAXBException {marshaller.marshal(object, writer);}public Object unmarshal(InputStream is) throws JAXBException {return unmarshaller.unmarshal(is);}}}
两个类,将内存中的xml信息持久化入文件 |
|