Package org.ribax.data

Source Code of org.ribax.data.DataUtils

/*
* RIBAX, Making Web Applications Easy
* Copyright (C) 2006 Damian Hamill and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY 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 along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/

package org.ribax.data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringReader;

import java.util.ArrayList;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

import org.ribax.utils.log.LOG;

import org.ribax.ConfigStrings;
import org.ribax.RIBAXConfig;
import org.ribax.datasources.DataSource;
import org.ribax.datasources.DataSourceFactory;
import org.ribax.parameters.ParameterSet;

/**
* Static utility methods for reading XML blocks from a web service.
*
* @version <tt>$Revision: $</tt>
* @author  <a href="mailto:damian@ribax.org">Damian Hamill</a>
*/

public class DataUtils {
 
  /**
     * Get an input stream to read from a web service.
     *
   * @param url the URL of the web service.
   * @param params a set of parameters to post to the web service.
   * @param name the name of the Object opening the stream (for debugging).
   * @return the input stream ready for reading
   * @throws IOException if an IO error occurs
   */
  public static InputStream getInputStream(String url,ArrayList params, String name) throws IOException {
      ArrayList tlist;
     
      if (params == null)
         params = new ArrayList();
     
      RIBAXConfig config = RIBAXConfig.getInstance();
       
      // add the base params
      tlist = (ArrayList)config.getValue(ConfigStrings.BASE_PARAMS,null);
     
      if (tlist != null)
        params.addAll(tlist);
     
        // add any global parameters
      if (ParameterSet.globalParameterSet != null) {      
        tlist = ParameterSet.globalParameterSet.getNameValuePairs();
        if (tlist != null)
          params.addAll(tlist);
      }
     
        // get a data source for the given URL
      DataSource  dataSource = DataSourceFactory.getDataSource(url,name);
     
      try {
            // get the input stream and return it
        return dataSource.getInputStream(params);
       
      } catch (IOException ex) {
        LOG.error(Messages.getString("DataUtils.0")+url,ex); //$NON-NLS-1$
        throw(ex);
     
  }
 
    /**
     * Get an Element that is an XML document root node from a web service.
     *
     * @param url the URL of the web service.
     * @param params a set of parameters to post to the web service.
     * @param name the name of the calling Object (for debugging).
     * @return the XML document root Element.
     * @throws IOException if an IO error occurs.
     */
  public static Element getDocumentRoot(String url,ArrayList params, String name) throws IOException {
   
    try {
            // get an input stream from the web service
      InputStream in = getInputStream(url,params,name);
     
      SAXBuilder builder = new SAXBuilder();

            // build the Element tree from the XML input
      Document doc = builder.build(in);
           
            // return the root element
      return doc.getRootElement();
     
    } catch (JDOMException ex) {
      // indicates a well-formedness error
      LOG.error(Messages.getString("DataUtils.1")+url,ex);       //$NON-NLS-1$
    } catch (IOException ex) {
      LOG.error(Messages.getString("DataUtils.2")+url,ex); //$NON-NLS-1$
    }
    return null;
  }

  /**
     * Check a block of text to see if one of a set of end tags occur in the text.  The
     * array of end tag names are the simple names without the '</>' as in 'html' rather
     * than '</html>'.
     *
   * @param block the text to examine
   * @param triggers an array of end tag names
   * @return
   */
  private static boolean triggersInBlock(String block, String[] triggers) {
   
    for(String s : triggers) {
      String endTrigger = "</" + s + ">"; //$NON-NLS-1$ //$NON-NLS-2$
      if (block.indexOf(endTrigger) >= 0)
        return true;
    }
    return false;
  }
  /** Read lines of text from an input stream looking for any 1 of a set of end tags.  The
     * end tags are in simple name format, i.e. 'html' rather than '</html>'.
     *
   * @param url the URL of a web service that we are reading from.
   * @param inStream an open input stream from the web service.
   * @param triggers an array of end tag names
   * @return
   */
  public static Element readElementBlock(String url,BufferedReader bin,
      String[] triggers) {
   
   
    try {
           
      SAXBuilder builder = new SAXBuilder();
     
            // a string buffer that holds the input until an end tag is found
      StringBuffer buf = new StringBuffer();
     
      if (LOG.isDebugEnabled())
        LOG.debug(Messages.getString("DataUtils.5")); //$NON-NLS-1$
     
      String line;
     
            // read lines from the input stream looking for the end tags
      do {
        line = bin.readLine();
        buf.append(line);
      } while(line != null && triggersInBlock(line,triggers) == false);
     
      if (LOG.isDebugEnabled())
        LOG.debug(Messages.getString("DataUtils.6")); //$NON-NLS-1$
     
            // create an XML document from the input text
      Document doc = builder.build(new StringReader(buf.toString()));
     
      if (LOG.isDebugEnabled())
        LOG.debug(Messages.getString("DataUtils.7")); //$NON-NLS-1$

            // return the document root Element
        return doc.getRootElement();
       
     } catch (IOException ex) {
       LOG.error(Messages.getString("DataUtils.8")+url,ex); //$NON-NLS-1$
     } catch (JDOMException ex) {
       // indicates a well-formedness error
       LOG.error(Messages.getString("DataUtils.9")+url,ex); //$NON-NLS-1$
     }
    
     return null;
  }
}
TOP

Related Classes of org.ribax.data.DataUtils

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.