Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2014 Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany.
   * All rights reserved. This program and the accompanying materials
   * are made available under the terms of the Creative Commons Attribution-NoDerivatives 4.0 International (CC BY-ND 4.0)
   * which accompanies this distribution, and is available at http://creativecommons.org/licenses/by-nd/4.0/
   *
   * Contributors:
   *      Leibniz Institute of Plant Genetics and Crop Plant Research (IPK), Gatersleben, Germany - RMI Client, FileChooser and WebDAV
   */
 package de.ipk_gatersleben.bit.bi.edal.webdav.wrapper.metadata;
 
 import java.io.File;
 
 
 
 public class ExtXMLConfig {
 	private JAXBContext context;
 	private Marshaller m;
 	private Unmarshaller um;
 	private Schema schema = null;

Creates an ExtXMLConfig-object, which uses rootClass as object to parse and save XML-files.

Parameters:
rootClass the class use create/parse xml-files from
Throws:
javax.xml.bind.JAXBException
 
 	public ExtXMLConfig(Class<?> rootClassthrows JAXBException {
 		 = JAXBContext.newInstance(rootClass);
 		init();
 	}

Creates an ExtXMLConfig, which uses a classPath like javax.xml.bin to use all classes in that path to parse and write xml-files

Parameters:
classPath the class path containing all needed java-objects
Throws:
javax.xml.bind.JAXBException
 
 	public ExtXMLConfig(String classPaththrows JAXBException {
 		 = JAXBContext.newInstance(classPath);
 		init();
 	}

Parses a xml-file into a JavaObject.

Parameters:
xml File-object representing the xml-file
Returns:
a java-Object
 
 	public Object load(File xml) {
 
 		if (xml.exists() && xml.isFile()) {
 			try {
 				return .unmarshal(xml);
 			} catch (JAXBException e) {
 				..error("Failed to load file: " + xml
 						+ "\nReason: " + e.toString());
 			}
 		} else {
 			..error("Failed to open file: " + xml
 					+ "\nReason: " + "file doesn't exists or is not a file.");
 		}
 
 		return null;
 	}
 
 	public Object load(InputStream inputStream) {
 
 		try {
 			return .unmarshal(inputStream);
 		} catch (JAXBException e) {
 			..error("Failed to load xml from stream: "
 					+ e.toString());
 		}
 
 		return null;
 	}
 
 	public Object load(String fileInput) {
 
		try {
			return .unmarshal(new StringReader(fileInput));
catch (JAXBException e) {
			..error("Failed to load xml from string: "
fileInput + "\nReason: " + e.toString());
		}
		return null;
	}

Saves a object into a xml-file.

Parameters:
xml the object to save
file path to the file to save to
	public void save(Object xmlString file) {
		save(xmlnew File(file));
	}

Saves a object into a xml-file.

Parameters:
xml the object to save
file File-object representing the file to save to
	public void save(Object xmlFile file) {
		try {
			.setProperty("jaxb.formatted.output"true);
catch (PropertyException e1) {
			// if not possible, output unformated
					.error("Failed to set formated output property: "
"\nReason: " + e1.toString());
		}
		if (!file.isDirectory()) {
			try {
				if (!file.exists()) {
				}
				.marshal(xmlfile);
catch (JAXBException e) {
				..error("Failed to save file: " + file
"\nReason: " + e.toString());
catch (IOException e) {
				..error("Failed to save file: " + file
"\nReason: " + e.toString());
			}
		}
	}

Returns a formatted string representation of a xml-file given as a java-Object.

Parameters:
xml the java-object to parse the xml from.
Returns:
a formatted string representation of the given object
	public String toString(Object xml) {
		StringWriter out = new StringWriter();
		try {
			.marshal(xmlout);
			// String ret = out.toString();
			// if(windows) {
			// ret.replaceAll("\n", "\r\n");
			// }
			//
			// return ret;
			return out.toString();
catch (JAXBException e) {
					.error("Failed to generate String representation for: "
xml + "\nReason: " + e.toString());
		}
		return null;
	}
	// private methods
	private void init() throws JAXBException {
	}
New to GrepCode? Check out our FAQ X