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

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

/**
 * 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.BufferedReader;
import java.io.IOException;
import java.io.StringReader;

import org.apache.log4j.Logger;

import com.novartis.opensource.yada.YADAQueryConfigurationException;
import com.novartis.opensource.yada.YADAQueryResult;

/**
 * Primarily a tool to assist with query authoring, this response returns query results wrapped in a simple HTML table.
 * @author David Varon
 *
 */
public class HTMLResponse extends AbstractResponse {
	/**
	 * Local logger handle
	 */
	@SuppressWarnings("unused")
	private static Logger l = Logger.getLogger(HTMLResponse.class);
	/**
   * Ivar containing the result to be returned by this class's {@link #toString()} method
   */
  private StringBuffer buffer = new StringBuffer();
	
	
	@Override
  public Response compose(YADAQueryResult[] yqrs) throws YADAConverterException, YADAResponseException, YADAQueryConfigurationException 
	{
	  Response delimitedResponse = new DelimitedResponse();
	  String csv = delimitedResponse.compose(yqrs).toString();

	  try(BufferedReader br = new BufferedReader(new StringReader(csv)))
	  {
	    String line = "";
	    int lineNum = 0;
	    int fields  = 0;
	    int hdrFields = 0;
	    String oTag = "";
	    String cTag = "";
	    String oRow = "";
	    String cRow = "\n";
	    this.buffer.append("\n\n\n\n\n");
	    this.buffer.append("\n");
	    while((line = br.readLine()) != null)
	    {
	      fields = 0;
	      if(lineNum == 0)
	      {
	        this.buffer.append("\n");
	      }
	      else
	      {
	        oTag = "";
	      }
  	    this.buffer.append(oRow);
	      char[] p = line.toCharArray(); // the values passed in
	      boolean escape = false, delim = false, inField = false;
	      // "Z,Z",,,"2013-04-03","2013-04-04 20:12:10" 
	      for(int i=0;i\n\n");
          hdrFields = fields;
          oTag = "";
        }
        lineNum++;
	    }
	    this.buffer.append("\n
"; cTag = "
"; cTag = "
\n\n"); } catch(IOException e) { String msg = "There was a problem parsing the result."; throw new YADAResponseException(msg, e); } return this; } /** * Returns the internal string * @see java.lang.Object#toString() */ @Override public String toString() { return this.buffer.toString(); } /** * Returns the internal string * @see java.lang.Object#toString() */ @Override public String toString(boolean prettyPrint) throws YADAResponseException { return this.toString(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy