Coverage Report - net.objectlab.qalab.m2.util.XmlTransformer
 
Classes in this File Line Coverage Branch Coverage Complexity
XmlTransformer
100% 
100% 
2
 
 1  
 ////////////////////////////////////////////////////////////////////////////////
 2  
 //
 3  
 //                  ObjectLab is sponsoring QALab
 4  
 //
 5  
 // Based in London, we are world leaders in the design and development
 6  
 // of bespoke applications for the Securities Financing markets.
 7  
 //
 8  
 // <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
 9  
 //           ___  _     _           _   _          _
 10  
 //          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
 11  
 //         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
 12  
 //         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
 13  
 //          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
 14  
 //                   |__/
 15  
 //
 16  
 //                   http://www.ObjectLab.co.uk
 17  
 // ---------------------------------------------------------------------------
 18  
 //
 19  
 //QALab is released under the GNU General Public License.
 20  
 //
 21  
 //QALab: Collects QA Statistics from your build over time.
 22  
 //2005+, ObjectLab Ltd
 23  
 //
 24  
 //This library is free software; you can redistribute it and/or
 25  
 //modify it under the terms of the GNU General Public
 26  
 //License as published by the Free Software Foundation; either
 27  
 //version 2.1 of the License, or (at your option) any later version.
 28  
 //
 29  
 //This library is distributed in the hope that it will be useful,
 30  
 //but WITHOUT ANY WARRANTY; without even the implied warranty of
 31  
 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 32  
 //General Public License for more details.
 33  
 //
 34  
 //You should have received a copy of the GNU General Public
 35  
 //License along with this library; if not, write to the Free Software
 36  
 //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 37  
 //
 38  
 ////////////////////////////////////////////////////////////////////////////////
 39  
 package net.objectlab.qalab.m2.util;
 40  
 
 41  
 import javax.xml.transform.Source;
 42  
 import javax.xml.transform.Transformer;
 43  
 import javax.xml.transform.TransformerFactory;
 44  
 import javax.xml.transform.TransformerException;
 45  
 
 46  
 import javax.xml.transform.stream.StreamSource;
 47  
 import javax.xml.transform.stream.StreamResult;
 48  
 
 49  
 import java.io.File;
 50  
 import java.io.InputStream;
 51  
 
 52  
 import java.util.Map;
 53  
 import java.util.HashMap;
 54  
 import java.util.Iterator;
 55  
 
 56  
 /**
 57  
  * A simple XML Transformer that is supplied with an XML file, an XSLT
 58  
  * file and an output file.
 59  
  * @author <a href="http://www.davesag.com">Dave Sag</a>.
 60  
  */
 61  98
 public class XmlTransformer {
 62  
 
 63  
     /** the XMl source file. */
 64  
     private final Source theXML;
 65  
 
 66  
     /** the XSL source file. */
 67  
     private final Source theXSLT;
 68  
 
 69  
     /** the output file. */
 70  
     private final StreamResult theOutput;
 71  
 
 72  
     /** the map of parameters. */
 73  
     private transient Map theParameters;
 74  
 
 75  
     /**
 76  
      * Constructor that takes the xml and xslt input stream and output file.
 77  
      * @param anXml The file containing well formed XML to be transformed.
 78  
      * @param anXslt The file containing the XSLT that tranforms the XML.
 79  
      * @param anOutput The file to write the outout to.
 80  
      */
 81  
     public XmlTransformer(final InputStream anXml,
 82  147
             final InputStream anXslt, final File anOutput) {
 83  147
         assert anXml != null : "The supplied XML file was null.";
 84  98
         assert anXslt != null : "The supplied XSLT file was null.";
 85  98
         assert anOutput != null : "The supplied output file was null.";
 86  
 
 87  98
         anOutput.getParentFile().mkdirs();
 88  
 
 89  98
         theXML = new StreamSource(anXml);
 90  98
         theXSLT = new StreamSource(anXslt);
 91  98
         theOutput = new StreamResult(anOutput);
 92  98
     }
 93  
 
 94  
     /**
 95  
      * Add an optional XSLT parameter.
 96  
      * @param aKey The key. a two-part string, the namespace URI enclosed in
 97  
      * curly braces ({}), followed by the local name. If the name has a
 98  
      * null URL, the String only contain the local name.
 99  
      * @param aValue The value.
 100  
      */
 101  
     public final void addParameter(final String aKey, final String aValue) {
 102  147
         if (theParameters == null) {
 103  49
             theParameters = new HashMap();
 104  
         }
 105  147
         theParameters.put(aKey, aValue);
 106  147
     }
 107  
 
 108  
     /**
 109  
      * Performs the XSLT transformation.
 110  
      * @throws TransformerException if anything went wrong.
 111  
      */
 112  
     public final void transform() throws TransformerException {
 113  
 
 114  98
         final TransformerFactory tf = TransformerFactory.newInstance();
 115  98
         final Transformer t = tf.newTransformer(theXSLT);
 116  
 
 117  
         // apply any parameters.
 118  98
         if (theParameters != null && !theParameters.isEmpty()) {
 119  49
             for (final Iterator i = theParameters.keySet().iterator();
 120  196
                     i.hasNext();) {
 121  147
                 final String key = (String) i.next();
 122  147
                 t.setParameter(key, theParameters.get(key));
 123  147
             }
 124  
         }
 125  
 
 126  98
         t.transform(theXML, theOutput);
 127  98
     }
 128  
 }
 129  
 /*
 130  
  *                   ObjectLab is sponsoring QALab
 131  
  * 
 132  
  * Based in London, we are world leaders in the design and development 
 133  
  * of bespoke applications for the securities financing markets.
 134  
  * 
 135  
  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
 136  
  *           ___  _     _           _   _          _
 137  
  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
 138  
  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
 139  
  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
 140  
  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
 141  
  *                   |__/
 142  
  *
 143  
  *                     www.ObjectLab.co.uk
 144  
  */