All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vectorprint.configuration.jaxb.SettingsXMLHelper Maven / Gradle / Ivy

Go to download

This project is about configuration of applications and about parameterization of Objects. This library offers annotations (and annotation processors), parsers, typing, observing changes, serialization, cloning and more when working with settings and/or object parameters. Settings and its features can be declared using an xml format. The library offers syntax support for settings and parameters in a loosely coupled manner. You are not restricted to built in syntax, you can provide your own. At runtime this library tracks keys for which a default is used because they are not found in settings. Also it tracks unused keys. You can stack features for settings such as caching, preparing keys and values, readonlyness, threadsafety, helpsupport, reading / parsing from input. You can easily develop your own features for settings.

There is a newer version: 12.2
Show newest version
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vectorprint.configuration.jaxb;

/*
 * #%L
 * VectorPrintReport
 * %%
 * Copyright (C) 2012 - 2014 VectorPrint
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
import com.vectorprint.IOHelper;
import com.vectorprint.configuration.generated.jaxb.Settingstype;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.sax.SAXSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Threadsafe helper for translating xml to JAXB objects
 * @author Eduard Drenth at VectorPrint.nl
 */
public class SettingsXMLHelper {

   private static Logger logger = Logger.getLogger(SettingsXMLHelper.class.getName());

   public static final String XSD = "/xsd/Settings.xsd";

   private static JAXBContext JAXBCONTEXT = null;
   private static Schema schema = null;

   static {
      try {
         SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
         schema = sf.newSchema(new SAXSource(new InputSource(SettingsXMLHelper.class.getResourceAsStream(XSD))));
         JAXBCONTEXT = JAXBContext.newInstance("com.vectorprint.configuration.generated.jaxb");
      } catch (JAXBException ex) {
         logger.log(Level.SEVERE, "failed to load jaxb context", ex);
      } catch (SAXException ex) {
         logger.log(Level.SEVERE, "failed to load schema", ex);
      }
   }

   public static Settingstype fromXML(Reader xml) throws JAXBException {
      Unmarshaller um = JAXBCONTEXT.createUnmarshaller();
      um.setSchema(schema);
      JAXBElement jb = (JAXBElement) um.unmarshal(xml);
      return (Settingstype) jb.getValue();
   }

   public static JAXBContext getJAXBCONTEXT() {
      return JAXBCONTEXT;
   }

   public static Schema getSchema() {
      return schema;
   }

   public static void validateXml(InputStream xml) throws SAXException, IOException {
      schema.newValidator().validate(new SAXSource(new InputSource(xml)));
   }

   public static void validateXml(Reader xml) throws SAXException, IOException {
      schema.newValidator().validate(new SAXSource(new InputSource(xml)));
   }

   public static void validateXml(URL xml) throws SAXException, IOException {
      validateXml(xml.openStream());
   }

   public static void validateXml(String xml) throws SAXException, IOException {
      validateXml(new StringReader(xml));
   }

   /**
    * prints xsd, or validate xml (no exceptions => ok)
    *
    * @see #VALIDATE
    * @param args
    * @throws IOException
    */
   public static void main(String[] args) throws IOException, SAXException {
      if (args != null && args.length > 0) {
         validateXml(new URL(args[0]));
      } else {
         System.out.println(getXsd());
      }
   }

   public static String getXsd() throws IOException {
      ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
      IOHelper.load(SettingsXMLHelper.class.getResourceAsStream(XSD), out);
      return out.toString();
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy