Package org.jboss.narayana.blacktie.jatmibroker.nbf

Source Code of org.jboss.narayana.blacktie.jatmibroker.nbf.NestedBufferParser

/* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat, Inc., and others contributors as indicated
* by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General public  License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE.  See the GNU Lesser General public  License for more details.
* You should have received a copy of the GNU Lesser General public  License,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA  02110-1301, USA.
*/
package org.jboss.narayana.blacktie.jatmibroker.nbf;

import java.io.ByteArrayInputStream;
import java.io.File;

import javax.xml.XMLConstants;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.Connection;
import org.jboss.narayana.blacktie.jatmibroker.xatmi.ConnectionException;
import org.xml.sax.SAXException;

import org.apache.xerces.xs.PSVIProvider;

public class NestedBufferParser {
  private NestedBufferHandlers handler;
  private SAXParser saxParser;
  private Schema schema;

  private static final Logger log = LogManager.getLogger(NestedBufferParser.class);
 
  public NestedBufferParser(String xsdFilename) throws ConnectionException {
    try {
      // Obtain a new instance of a SAXParserFactory.
      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setNamespaceAware(true);
      factory.setValidating(true);
      SchemaFactory schemaFactory = SchemaFactory
      .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      factory.setFeature("http://apache.org/xml/features/validation/schema",
          true);

      File file = new File(xsdFilename);
      if (file.exists()) {
        schema = schemaFactory.newSchema(file);
      } else {
        throw new ConnectionException(Connection.TPEOS,
              "Could not find " + xsdFilename);
      }
     
      factory.setSchema(schema);
      saxParser = factory.newSAXParser();
      PSVIProvider p = (PSVIProvider) saxParser.getXMLReader();
     
      handler = new NestedBufferHandlers(p);

    }catch (SAXException e) {
      log.error("Could not create a SAXParser: " + e.getMessage(), e);
      throw new ConnectionException(Connection.TPEOS,
            "Could not create a SAXParser: " + e.getMessage());
    } catch (ParserConfigurationException e) {
      log.error("Could not create a SAXParser: " + e.getMessage(), e);
      throw new ConnectionException(Connection.TPEOS,
            "Could not create a SAXParser: " + e.getMessage());
    } catch (Throwable e) {
      log.error("Could not create a SAXParser: " + e.getMessage(), e);
      throw new ConnectionException(Connection.TPEOS,
            "Could not create a SAXParser: " + e.getMessage());
    }
  }
 
  public void setId(String id) {
    handler.setId(id);
  }
 
  public void setIndex(int index) {
    handler.setIndex(index);
  }
 
  public String getType() {
    return handler.getType();
  }
 
  public String getValue() {
    return handler.getValue();
  }
 
  public boolean parse(byte[] buffer) throws ConnectionException {
    boolean result = false;
   
    try
      schema.newValidator().validate(new StreamSource(
          new ByteArrayInputStream(buffer)));
      saxParser.parse(new ByteArrayInputStream(buffer), handler);
      result = true;
    } catch (Throwable e) {
      log.error("Parser buffer failed with " + e.getMessage(), e);
      throw new ConnectionException(Connection.TPEOS,
          "Parser buffer failed with " + e.getMessage());
    }
   
    return result;
  }
}
TOP

Related Classes of org.jboss.narayana.blacktie.jatmibroker.nbf.NestedBufferParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.