JAVA   71
StaxComplianceCheck
Guest on 13th August 2022 08:17:41 AM


  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.logging.Logger;
  6. import java.util.zip.GZIPInputStream;
  7. import javax.xml.stream.XMLInputFactory;
  8. import javax.xml.stream.XMLStreamConstants;
  9. import javax.xml.stream.XMLStreamException;
  10. import javax.xml.stream.XMLStreamReader;
  11. import javax.xml.transform.stream.StreamSource;
  12.  
  13. /**
  14.  * $Id: Bla.java 403 Z elkner $
  15.  *
  16.  * Copyright (c)  Jens Elkner.
  17.  * All Rights Reserved.
  18.  *
  19.  * This software is the proprietary information of Jens Elkner.
  20.  * Use is subject to license terms.
  21.  */
  22. /**
  23.  * @author      Jens Elkner
  24.  * @version     $Revision: 403 $
  25.  */
  26. public class StaxComplianceCheck {
  27.         private static final Logger log =
  28.                 Logger.getLogger(StaxComplianceCheck.class.getName());
  29.         static Boolean coalescing = Boolean.TRUE;
  30.        
  31.         /**
  32.          * boilerplate
  33.          * @param path
  34.          * @param gzip
  35.          * @return stream src
  36.          * @throws IOException
  37.          */
  38.         public static StreamSource getInputSourceByFile(File path, boolean gzip)
  39.         throws IOException
  40.         {
  41.                 InputStream is = null;
  42.                 try {
  43.                         is = new FileInputStream(path);
  44.                         if (gzip) {
  45.                                 is = new GZIPInputStream(is);
  46.                         }
  47.                         return new StreamSource(is, path.getName());
  48.                 } catch (IOException e) {
  49.                         if (is != null) {
  50.                                 try { is.close(); } catch (Exception x) { /** ignore */ }
  51.                         }
  52.                         throw e;
  53.                 }
  54.         }
  55.  
  56.         /**
  57.          * boilerplate
  58.          * @param reader
  59.          * @throws XMLStreamException
  60.          */
  61.         public static void fastForwardToEndOfElement(XMLStreamReader reader)
  62.                 throws XMLStreamException
  63.         {
  64.                 if (reader == null) {
  65.                         return;
  66.                 }
  67.                 int starts = 1;
  68.                 while(reader.hasNext()) {
  69.                         int e = reader.next();
  70.                         if (e == XMLStreamConstants.END_ELEMENT) {
  71.                                 starts--;
  72.                                 if (starts == 0) {
  73.                                         return;
  74.                                 }
  75.                         } else if (e == XMLStreamConstants.START_ELEMENT) {
  76.                                 starts++;
  77.                         }
  78.                 }
  79.                 log.warning("Missing " + starts + " End Of Element tags");
  80.         }
  81.  
  82.         /**
  83.          * boilerplate
  84.          * @param src
  85.          * @param root
  86.          * @param namespaceAware
  87.          * @return null on error
  88.          */
  89.         public static final XMLStreamReader getReader(StreamSource src,
  90.                 String root, boolean namespaceAware)
  91.         {
  92.                 if (src == null) {
  93.                         return null;
  94.                 }
  95.                 XMLInputFactory xif = XMLInputFactory.newInstance();
  96.                 xif.setProperty(XMLInputFactory.IS_COALESCING, coalescing);
  97.                 xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
  98.                         Boolean.TRUE);
  99.                 xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE,
  100.                         Boolean.valueOf(namespaceAware));
  101.                 xif.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE);
  102. //              xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
  103. //                      Boolean.FALSE);
  104.                 try {
  105.                         XMLStreamReader reader = xif.createXMLStreamReader(src);
  106.                         while(reader.hasNext()) {
  107.                                 int e = reader.next();
  108.                                 if (e == XMLStreamConstants.START_ELEMENT) {
  109.                                         if (root == null || reader.getLocalName().equals(root)) {
  110.                                                 return reader;
  111.                                         }
  112.                                         fastForwardToEndOfElement(reader);
  113.                                 }
  114.                         }
  115.                 } catch (XMLStreamException e) {
  116.                         if (e.getNestedException() != null) {
  117.                                 log.warning(e.getNestedException().getLocalizedMessage());
  118.                         } else {
  119.                                 log.warning(e.getLocalizedMessage());
  120.                         }
  121.                 } catch (Exception e) {
  122.                         log.warning(e.getLocalizedMessage());
  123.                 }
  124.                 return null;
  125.         }
  126.  
  127.         /**
  128.          * exception demo
  129.          * @param in
  130.          * @throws Exception
  131.          */
  132.         public static void parse(XMLStreamReader in) throws Exception {
  133.                 int depth = 1;
  134. //              String indent = "  ";
  135. //              StringBuilder buf = new StringBuilder(indent);
  136. //              System.out.println(in.getLocalName());
  137.                 while(in.hasNext()) {
  138.                         int e = in.next();
  139.                         if (e == XMLStreamConstants.END_ELEMENT) {
  140.                                 depth--;
  141. //                              buf.setLength(buf.length()-indent.length());
  142.                                 if (depth == 0) {
  143.                                         break;
  144.                                 }
  145.                         } else if (e == XMLStreamConstants.START_ELEMENT) {
  146.                                 depth++;
  147. //                              buf.append(indent);
  148. //                              System.out.print(in.getLocalName() + ": ");
  149.                                 String txt = in.getElementText();
  150. //                              System.out.println(txt);
  151.                                 depth--;
  152. //                              buf.setLength(buf.length()-indent.length());
  153.                         }
  154.                 }
  155.         }
  156.        
  157.         /**
  158.          * @param args  0 .. xml file to read
  159.          *              1 .. anything - disable coalescing, if not equal to true
  160.          *              2 .. anything - turn off hardwiring Sun Stax impl.
  161.          *              
  162.          * @throws Exception
  163.          */
  164.         public static void main(String[] args) throws Exception {
  165.                 File f = null;
  166.                 if (args.length < 3) {
  167.                         System.out.println("Using SUN's implementation");
  168.                         System.setProperty("javax.xml.stream.XMLInputFactory",
  169.                                 "com.sun.xml.internal.stream.XMLInputFactoryImpl");
  170.                         System.setProperty("javax.xml.stream.XMLEventFactory",
  171.                                 "com.sun.xml.internal.stream.events.XMLEventFactoryImpl");
  172.                         System.setProperty("javax.xml.stream.XMLOutputFactory",
  173.                                 "com.sun.xml.internal.stream.XMLOutputFactoryImpl");
  174.                 }
  175.                 if (args.length > 1) {
  176.                         coalescing = Boolean.valueOf(args[1].equals("true"));
  177.                 }
  178.                 if (args.length == 0) {
  179.                         f = new File("US.xml.gz");
  180.                 } else {
  181.                         f = new File(args[0]);
  182.                 }
  183.                 if (f.canRead() && f.isFile()) {
  184.                         StreamSource src = null;
  185.                         XMLStreamReader in = null;
  186.                         try {
  187.                                 src = getInputSourceByFile(f, f.getName().endsWith(".xml.gz"));
  188.                                 in = getReader(src, "GetAttributesCSResponse", false);
  189.                                 if (in != null) {
  190.                                         long start = System.currentTimeMillis();
  191.                                         parse(in);
  192.                                         long stop = System.currentTimeMillis();
  193.                                         System.out.println("time: " + (stop -start));
  194.                                 } else {
  195.                                         log.severe("GetAttributesCSResponse tag not found");
  196.                                 }
  197.                         } finally {
  198.                                 try { if (in != null) in.close(); } catch (Exception x) { /* ignore */ }
  199.                                 try { if (src != null) src.getInputStream().close(); } catch (Exception x) {/**/}
  200.                         }
  201.                 } else {
  202.                         System.err.println("Usage: java ... file.xml.gz");
  203.                 }
  204.         }
  205. }

Raw Paste

Login or Register to edit or fork this paste. It's free.