- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.logging.Logger;
- import java.util.zip.GZIPInputStream;
- import javax.xml.stream.XMLInputFactory;
- import javax.xml.stream.XMLStreamConstants;
- import javax.xml.stream.XMLStreamException;
- import javax.xml.stream.XMLStreamReader;
- import javax.xml.transform.stream.StreamSource;
- /**
- * $Id: Bla.java 403 Z elkner $
- *
- * Copyright (c) Jens Elkner.
- * All Rights Reserved.
- *
- * This software is the proprietary information of Jens Elkner.
- * Use is subject to license terms.
- */
- /**
- * @author Jens Elkner
- * @version $Revision: 403 $
- */
- public class StaxComplianceCheck {
- private static final Logger log =
- Logger.getLogger(StaxComplianceCheck.class.getName());
- /**
- * boilerplate
- * @param path
- * @param gzip
- * @return stream src
- * @throws IOException
- */
- throws IOException
- {
- try {
- if (gzip) {
- }
- return new StreamSource(is, path.getName());
- if (is != null) {
- }
- throw e;
- }
- }
- /**
- * boilerplate
- * @param reader
- * @throws XMLStreamException
- */
- public static void fastForwardToEndOfElement(XMLStreamReader reader)
- throws XMLStreamException
- {
- if (reader == null) {
- return;
- }
- int starts = 1;
- while(reader.hasNext()) {
- int e = reader.next();
- if (e == XMLStreamConstants.END_ELEMENT) {
- starts--;
- if (starts == 0) {
- return;
- }
- } else if (e == XMLStreamConstants.START_ELEMENT) {
- starts++;
- }
- }
- log.warning("Missing " + starts + " End Of Element tags");
- }
- /**
- * boilerplate
- * @param src
- * @param root
- * @param namespaceAware
- * @return null on error
- */
- public static final XMLStreamReader getReader(StreamSource src,
- {
- if (src == null) {
- return null;
- }
- XMLInputFactory xif = XMLInputFactory.newInstance();
- xif.setProperty(XMLInputFactory.IS_COALESCING, coalescing);
- xif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
- xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE,
- // xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
- // Boolean.FALSE);
- try {
- XMLStreamReader reader = xif.createXMLStreamReader(src);
- while(reader.hasNext()) {
- int e = reader.next();
- if (e == XMLStreamConstants.START_ELEMENT) {
- if (root == null || reader.getLocalName().equals(root)) {
- return reader;
- }
- fastForwardToEndOfElement(reader);
- }
- }
- } catch (XMLStreamException e) {
- if (e.getNestedException() != null) {
- log.warning(e.getNestedException().getLocalizedMessage());
- } else {
- log.warning(e.getLocalizedMessage());
- }
- log.warning(e.getLocalizedMessage());
- }
- return null;
- }
- /**
- * exception demo
- * @param in
- * @throws Exception
- */
- int depth = 1;
- // String indent = " ";
- // StringBuilder buf = new StringBuilder(indent);
- // System.out.println(in.getLocalName());
- while(in.hasNext()) {
- int e = in.next();
- if (e == XMLStreamConstants.END_ELEMENT) {
- depth--;
- // buf.setLength(buf.length()-indent.length());
- if (depth == 0) {
- break;
- }
- } else if (e == XMLStreamConstants.START_ELEMENT) {
- depth++;
- // buf.append(indent);
- // System.out.print(in.getLocalName() + ": ");
- // System.out.println(txt);
- depth--;
- // buf.setLength(buf.length()-indent.length());
- }
- }
- }
- /**
- * @param args 0 .. xml file to read
- * 1 .. anything - disable coalescing, if not equal to true
- * 2 .. anything - turn off hardwiring Sun Stax impl.
- *
- * @throws Exception
- */
- if (args.length < 3) {
- "com.sun.xml.internal.stream.XMLInputFactoryImpl");
- "com.sun.xml.internal.stream.events.XMLEventFactoryImpl");
- "com.sun.xml.internal.stream.XMLOutputFactoryImpl");
- }
- if (args.length > 1) {
- }
- if (args.length == 0) {
- } else {
- }
- if (f.canRead() && f.isFile()) {
- StreamSource src = null;
- XMLStreamReader in = null;
- try {
- src = getInputSourceByFile(f, f.getName().endsWith(".xml.gz"));
- in = getReader(src, "GetAttributesCSResponse", false);
- if (in != null) {
- parse(in);
- } else {
- log.severe("GetAttributesCSResponse tag not found");
- }
- } finally {
- }
- } else {
- }
- }
- }
Raw Paste