Java tutorial
/* * The MIT License * * Copyright 2015 Laurent Morissette. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package com.mor.blogengine.xml.io; //~--- non-JDK imports -------------------------------------------------------- import com.mor.blogengine.exception.IncorrectPropertyValueException; import com.mor.blogengine.exception.MissingPropertyException; import com.mor.common.PropertiesUserObject; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.net.URL; import java.nio.charset.Charset; import java.util.Properties; import javax.xml.parsers.ParserConfigurationException; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.xml.sax.SAXException; /** * * @author laurent */ public class XmlDataSourceProvider extends PropertiesUserObject { public static final String JAXP_SCHEMA_LANGUAGE = "http://java.sun.com/xml/jaxp/properties/schemaLanguage"; public static final String JAXP_SCHEMA_SOURCE = "http://java.sun.com/xml/jaxp/properties/schemaSource"; private Document mProvidedDoc = null; private URL xml = null; public XmlDataSourceProvider(Properties p) { mConfig = p; } /** * <pre> * Provide a Dom4J document from various source type * * * @return built document * </pre> * * @throws SAXException * @throws ParserConfigurationException * @throws DocumentException * @throws IOException */ public Document provide() throws SAXException, ParserConfigurationException, DocumentException, IOException, MissingPropertyException, IncorrectPropertyValueException { xml = getXml(); URL schema = getSchema(); if ((schema != null) && (xml != null)) { mProvidedDoc = createReaderAgainstSchema(schema).read(xml); } return mProvidedDoc; } /** * Setup validation features for given reader * * @param pReader given reader * @throws javax.xml.parsers.ParserConfigurationException * @throws org.xml.sax.SAXException * @throws java.io.IOException * @param schemaSource * @return */ private SAXReader createReaderAgainstSchema(URL schemaSource) throws SAXException, ParserConfigurationException, IOException, MissingPropertyException, IncorrectPropertyValueException { trace("Parser created OK"); SAXReader reader = new SAXReader(); trace("reader created OK"); trace("set reader properties"); // set the validation feature to true to report validation errors reader.setFeature("http://xml.org/sax/features/validation", true); // set the validation/schema feature to true to report validation errors against a schema reader.setFeature("http://apache.org/xml/features/validation/schema", true); //set the validation/schema-full-checking feature to true to enable full schema, grammar-constraint checking reader.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); trace("returning reader..."); return reader; } /** * writes in a file * * @param document DOM model to write in * @param pOutputFile output file * @throws java.io.IOException * @param pDocument */ boolean write(Document pDocument) throws MissingPropertyException, IncorrectPropertyValueException { boolean ret = false; try { OutputFormat format = new OutputFormat(); format.setEncoding(getFileEncoding()); XMLWriter writer; writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(xml.getFile()), Charset.forName(getFileEncoding()))); writer.write(pDocument); writer.close(); ret = true; } catch (IOException ex) { trace("Error saving file..." + ex); } return ret; } /** * Saves changes in dom structure * * @return true if saved correctly */ public boolean saveChanges() throws MissingPropertyException, IncorrectPropertyValueException { boolean saved = false; if (isPersistingNecessary()) { trace("saving..."); saved = write(mProvidedDoc); } else { trace("if persisting was set to on changes would be saved to file"); saved = true; } return saved; } } //~ Formatted by Jindent --- http://www.jindent.com