001    /**
002     * 
003     */
004    package org.wdssii.core;
005    
006    import java.io.FileOutputStream;
007    import java.io.IOException;
008    import java.io.OutputStream;
009    import java.util.zip.GZIPOutputStream;
010    
011    import javax.xml.transform.OutputKeys;
012    import javax.xml.transform.Result;
013    import javax.xml.transform.Source;
014    import javax.xml.transform.Transformer;
015    import javax.xml.transform.TransformerException;
016    import javax.xml.transform.TransformerFactory;
017    import javax.xml.transform.TransformerFactoryConfigurationError;
018    import javax.xml.transform.dom.DOMSource;
019    import javax.xml.transform.stream.StreamResult;
020    
021    import org.w3c.dom.Document;
022    import org.w3c.dom.Element;
023    
024    /**
025     * @author lakshman
026     * 
027     */
028    public class XMLUtil {
029    
030            public static boolean getBooleanAttribute(Element vcpel, String name,
031                            boolean defaultValue) {
032                    if (vcpel.hasAttribute(name)) {
033                            String value = vcpel.getAttribute(name);
034                            return (value.equals("true") || value.equals("yes") || value
035                                            .equals("1"));
036                    }
037                    return defaultValue;
038            }
039    
040            public static int getIntegerAttribute(Element vcpel, String name,
041                            int defaultValue) {
042                    if (vcpel.hasAttribute(name)) {
043                            String value = vcpel.getAttribute(name);
044                            return Integer.parseInt(value);
045                    }
046                    return defaultValue;
047            }
048    
049            public static void writeXmlFile(Document doc, String filename, boolean compress) throws IOException {
050            OutputStream os = null;
051                    try {
052                    // in
053                Source source = new DOMSource(doc);
054        
055                // out
056                if (compress){
057                    if (!filename.endsWith(".gz")){
058                            filename = filename + ".gz";
059                    }
060                    os = new GZIPOutputStream(new FileOutputStream(filename));
061                } else {
062                    os = new FileOutputStream(filename);
063                }
064                Result result = new StreamResult(os);
065        
066                // write
067                Transformer t = TransformerFactory.newInstance().newTransformer();
068                t.setOutputProperty(OutputKeys.INDENT, "yes");
069                t.transform(source, result);
070                
071            } catch (TransformerException e) {
072                    throw new IllegalStateException("Couldn't find TRaX API to write out XML");
073                    } catch (TransformerFactoryConfigurationError e) {
074                            throw new IllegalStateException("Couldn't find TRaX API to write out XML");
075                    } finally {
076                    if (os != null){
077                            os.close();
078                    }
079            }
080        }
081    }