All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.novartis.opensource.yada.format.SOAPResultXMLConverter Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2016 Novartis Institutes for BioMedical Research Inc.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.novartis.opensource.yada.format;

import java.io.IOException;
import java.io.StringReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import com.novartis.opensource.yada.YADAQueryResult;

/**
 * A converter subclass for traversal and appending of SOAP results to the calling Response object.
 * @author David Varon
 *
 */
public class SOAPResultXMLConverter extends AbstractConverter
{

	/**
	 * Local logger handle
	 */
	@SuppressWarnings("unused")
	private static Logger l = Logger.getLogger(RESTResultJSONConverter.class);

  /**
   * Java XML API component
   */
	private DocumentBuilderFactory docFactory;
  /**
   * Java XML API component
   */
	private DocumentBuilder        docBuilder;
  /**
   * Java XML API component
   */
	private Document               doc;
  /**
   * Java XML API component
   */
	private DocumentFragment       frag;
	
	/**
   * Default constructor
   */
  public SOAPResultXMLConverter() {
    // default constructor
  }
  
  /**
   * Constructor with {@link YADAQueryResult}
   * @param yqr the container for result processing artifacts
   */
  public SOAPResultXMLConverter(YADAQueryResult yqr) {
    this.setYADAQueryResult(yqr);
  }

	/**
	 * Returns {@code true} if the {@code node} is a {@link org.w3c.dom.Node#TEXT_NODE} and contains only whitespace
	 * @param node the node to check for whitespace only
	 * @return {@code true} if the {@code node} is a {@link org.w3c.dom.Node#TEXT_NODE} and contains only whitespace
	 */
	public boolean isWhitespace(Node node)
	{
		if(node.getNodeType() == Node.TEXT_NODE)
		{
			Pattern p = Pattern.compile("^[\\s]+$");
			Matcher m = p.matcher(node.getTextContent());
			if (m.matches())
				return true;
		}
		return false;	
	}
	
	/**
	 * Recurses through document, starting with {@code node}, incrementally appending nodes to the 
	 * {@link org.w3c.dom.DocumentFragment} which is returned by {@link #convert(Object)}
	 * @param node the node into which to drill down
	 */
	public void visitNodes(Node node) //soap:Body, frag
	{
		NodeList list = node.getChildNodes();
		if(list != null)
		{
			for (int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy