private byte[] formatField(ValueMetaInterface v, Object valueData) throws KettleValueException {
   if (v.isString()) {
     String svalue = (valueData instanceof String) ? (String) valueData : v.getString(valueData);
     return convertStringToBinaryString(v, Const.trimToType(svalue, v.getTrimType()));
   } else {
     return v.getBinaryString(valueData);
   }
 }
 /**
  * *************************************** formatting routines for "unformatted" output
  * ****************************************.
  *
  * @param valueMeta the value meta
  * @param valueData the value data
  * @return the byte[]
  * @throws KettleValueException the kettle value exception
  */
 static byte[] convertChar(ValueMetaInterface valueMeta, Object valueData)
     throws KettleValueException {
   String string = valueMeta.getString(valueData);
   if (string != null) {
     return (string.getBytes());
   }
   return null;
 }
Example #3
0
 private void writeField(ValueMetaInterface valueMeta, Object valueData, String element)
     throws KettleStepException {
   try {
     String str = XMLHandler.addTagValue(element, valueMeta.getString(valueData), false);
     if (str != null) data.writer.write(str.toCharArray());
   } catch (Exception e) {
     throw new KettleStepException("Error writing line :", e); // $NON-NLS-1$
   }
 }
  public static Object[] createObjectsForRow(RowMetaInterface rowMeta, Object[] rowData)
      throws KettleValueException {
    Object[] values = new Object[rowMeta.size()];
    for (int i = 0; i < rowMeta.size(); i++) {
      ValueMetaInterface valueMeta = rowMeta.getValueMeta(i);
      Object valueData = rowData[i];

      // Prevent a NullPointerException below
      if (valueData == null || valueMeta == null) {
        values[i] = null;
        continue;
      }

      int length = valueMeta.getLength();

      switch (valueMeta.getType()) {
        case ValueMetaInterface.TYPE_INTEGER:
          if (length < 3) {
            values[i] = new Byte(valueMeta.getInteger(valueData).byteValue());
          } else {
            if (length < 5) {
              values[i] = new Short(valueMeta.getInteger(valueData).shortValue());
            } else {
              values[i] = valueMeta.getInteger(valueData);
            }
          }
          break;
        case ValueMetaInterface.TYPE_NUMBER:
          values[i] = valueMeta.getNumber(valueData);
          break;
        case ValueMetaInterface.TYPE_DATE:
          values[i] = valueMeta.getDate(valueData);
          break;
        case ValueMetaInterface.TYPE_STRING:
          values[i] = valueMeta.getString(valueData);
          break;
        case ValueMetaInterface.TYPE_BINARY:
          values[i] = valueMeta.getBinary(valueData);
          break;
        case ValueMetaInterface.TYPE_BOOLEAN:
          values[i] = valueMeta.getBoolean(valueData);
          break;
        case ValueMetaInterface.TYPE_BIGNUMBER:
          values[i] = valueMeta.getNumber(valueData);
          break;
        default:
          break;
      }
    }
    return values;
  }
  protected int getDataForRow(TableItem item, Object[] row) {
    int nrErrors = 0;

    // Display the correct line item...
    //
    String strNr;
    lineNr++;
    try {
      strNr = wFields.getNumberColumn().getValueMeta().getString(new Long(lineNr));
    } catch (Exception e) {
      strNr = Integer.toString(lineNr);
    }
    item.setText(0, strNr);

    for (int c = 0; c < rowMeta.size(); c++) {
      ValueMetaInterface v = rowMeta.getValueMeta(c);
      String show;
      try {
        show = v.getString(row[c]);
        if (v.isBinary() && show != null && show.length() > MAX_BINARY_STRING_PREVIEW_SIZE) {
          // We want to limit the size of the strings during preview to keep all SWT widgets happy.
          //
          show = show.substring(0, MAX_BINARY_STRING_PREVIEW_SIZE);
        }
      } catch (KettleValueException e) {
        nrErrors++;
        if (nrErrors < 25) {
          log.logError(Const.getStackTracker(e));
        }
        show = null;
      } catch (ArrayIndexOutOfBoundsException e) {
        nrErrors++;
        if (nrErrors < 25) {
          log.logError(Const.getStackTracker(e));
        }
        show = null;
      }

      if (show != null) {
        item.setText(c + 1, show);
      }
    }

    return nrErrors;
  }
Example #6
0
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    if (isJettyMode() && !request.getContextPath().startsWith(CONTEXT_PATH)) {
      return;
    }

    if (log.isDebug()) {
      logDebug(BaseMessages.getString(PKG, "TransStatusServlet.Log.SniffStepRequested"));
    }

    String transName = request.getParameter("trans");
    String id = request.getParameter("id");
    String stepName = request.getParameter("step");
    int copyNr = Const.toInt(request.getParameter("copynr"), 0);
    final int nrLines = Const.toInt(request.getParameter("lines"), 0);
    String type = Const.NVL(request.getParameter("type"), TYPE_OUTPUT);
    boolean useXML = "Y".equalsIgnoreCase(request.getParameter("xml"));

    response.setStatus(HttpServletResponse.SC_OK);

    if (useXML) {
      response.setContentType("text/xml");
      response.setCharacterEncoding(Const.XML_ENCODING);
    } else {
      response.setContentType("text/html;charset=UTF-8");
    }

    PrintWriter out = response.getWriter();

    // ID is optional...
    //
    Trans trans;
    CarteObjectEntry entry;
    if (Const.isEmpty(id)) {
      // get the first transformation that matches...
      //
      entry = getTransformationMap().getFirstCarteObjectEntry(transName);
      if (entry == null) {
        trans = null;
      } else {
        id = entry.getId();
        trans = getTransformationMap().getTransformation(entry);
      }
    } else {
      // Take the ID into account!
      //
      entry = new CarteObjectEntry(transName, id);
      trans = getTransformationMap().getTransformation(entry);
    }

    Encoder encoder = ESAPI.encoder();

    if (trans != null) {

      // Find the step to look at...
      //
      StepInterface step = null;
      List<StepInterface> stepInterfaces = trans.findBaseSteps(stepName);
      for (int i = 0; i < stepInterfaces.size(); i++) {
        StepInterface look = stepInterfaces.get(i);
        if (look.getCopy() == copyNr) {
          step = look;
        }
      }
      if (step != null) {

        // Add a listener to the transformation step...
        //
        final boolean read = type.equalsIgnoreCase(TYPE_INPUT);
        final boolean written = type.equalsIgnoreCase(TYPE_OUTPUT) || !read;
        final MetaAndData metaData = new MetaAndData();

        metaData.bufferRowMeta = null;
        metaData.bufferRowData = new ArrayList<Object[]>();

        RowListener rowListener =
            new RowListener() {
              public void rowReadEvent(RowMetaInterface rowMeta, Object[] row)
                  throws KettleStepException {
                if (read && metaData.bufferRowData.size() < nrLines) {
                  metaData.bufferRowMeta = rowMeta;
                  metaData.bufferRowData.add(row);
                }
              }

              public void rowWrittenEvent(RowMetaInterface rowMeta, Object[] row)
                  throws KettleStepException {
                if (written && metaData.bufferRowData.size() < nrLines) {
                  metaData.bufferRowMeta = rowMeta;
                  metaData.bufferRowData.add(row);
                }
              }

              public void errorRowWrittenEvent(RowMetaInterface rowMeta, Object[] row)
                  throws KettleStepException {}
            };

        step.addRowListener(rowListener);

        // Wait until we have enough rows...
        //
        while (metaData.bufferRowData.size() < nrLines
            && step.isRunning()
            && !trans.isFinished()
            && !trans.isStopped()) {

          try {
            Thread.sleep(100);
          } catch (InterruptedException e) {
            // Ignore
            //
            break;
          }
        }

        // Remove the row listener
        //
        step.removeRowListener(rowListener);

        // Pass along the rows of data...
        //
        if (useXML) {

          // Send the result back as XML
          //
          response.setContentType("text/xml");
          response.setCharacterEncoding(Const.XML_ENCODING);
          out.print(XMLHandler.getXMLHeader(Const.XML_ENCODING));

          out.println(XMLHandler.openTag(XML_TAG));

          if (metaData.bufferRowMeta != null) {

            // Row Meta data
            //
            out.println(metaData.bufferRowMeta.getMetaXML());

            // Nr of lines
            //
            out.println(XMLHandler.addTagValue("nr_rows", metaData.bufferRowData.size()));

            // Rows of data
            //
            for (int i = 0; i < metaData.bufferRowData.size(); i++) {
              Object[] rowData = metaData.bufferRowData.get(i);
              out.println(metaData.bufferRowMeta.getDataXML(rowData));
            }
          }

          out.println(XMLHandler.closeTag(XML_TAG));

        } else {
          response.setContentType("text/html;charset=UTF-8");

          out.println("<HTML>");
          out.println("<HEAD>");
          out.println(
              "<TITLE>"
                  + BaseMessages.getString(PKG, "SniffStepServlet.SniffResults")
                  + "</TITLE>");
          out.println(
              "<META http-equiv=\"Refresh\" content=\"10;url="
                  + convertContextPath(CONTEXT_PATH)
                  + "?name="
                  + URLEncoder.encode(transName, "UTF-8")
                  + "&id="
                  + URLEncoder.encode(id, "UTF-8")
                  + "\">");
          out.println("<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">");
          out.println("</HEAD>");
          out.println("<BODY>");
          out.println(
              "<H1>"
                  + encoder.encodeForHTML(
                      BaseMessages.getString(PKG, "SniffStepServlet.SniffResultsForStep", stepName))
                  + "</H1>");

          try {
            out.println("<table border=\"1\">");

            if (metaData.bufferRowMeta != null) {
              // Print a header row containing all the field names...
              //
              out.print("<tr><th>#</th>");
              for (ValueMetaInterface valueMeta : metaData.bufferRowMeta.getValueMetaList()) {
                out.print("<th>" + valueMeta.getName() + "</th>");
              }
              out.println("</tr>");

              // Now output the data rows...
              //
              for (int r = 0; r < metaData.bufferRowData.size(); r++) {
                Object[] rowData = metaData.bufferRowData.get(r);
                out.print("<tr>");
                out.println("<td>" + (r + 1) + "</td>");
                for (int v = 0; v < metaData.bufferRowMeta.size(); v++) {
                  ValueMetaInterface valueMeta = metaData.bufferRowMeta.getValueMeta(v);
                  Object valueData = rowData[v];
                  out.println("<td>" + valueMeta.getString(valueData) + "</td>");
                }
                out.println("</tr>");
              }
            }

            out.println("</table>");

            out.println("<p>");

          } catch (Exception ex) {
            out.println("<p>");
            out.println("<pre>");
            out.println(encoder.encodeForHTML(Const.getStackTracker(ex)));
            out.println("</pre>");
          }

          out.println("<p>");
          out.println("</BODY>");
          out.println("</HTML>");
        }
      } else {
        if (useXML) {
          out.println(
              new WebResult(
                      WebResult.STRING_ERROR,
                      BaseMessages.getString(
                          PKG, "SniffStepServlet.Log.CoundNotFindSpecStep", stepName))
                  .getXML());
        } else {
          out.println(
              "<H1>"
                  + encoder.encodeForHTML(
                      BaseMessages.getString(
                          PKG, "SniffStepServlet.Log.CoundNotFindSpecStep", stepName))
                  + "</H1>");
          out.println(
              "<a href=\""
                  + convertContextPath(GetStatusServlet.CONTEXT_PATH)
                  + "\">"
                  + BaseMessages.getString(PKG, "TransStatusServlet.BackToStatusPage")
                  + "</a><p>");
        }
      }
    } else {
      if (useXML) {
        out.println(
            new WebResult(
                    WebResult.STRING_ERROR,
                    BaseMessages.getString(
                        PKG, "SniffStepServlet.Log.CoundNotFindSpecTrans", transName))
                .getXML());
      } else {
        out.println(
            "<H1>"
                + encoder.encodeForHTML(
                    BaseMessages.getString(
                        PKG, "SniffStepServlet.Log.CoundNotFindTrans", transName))
                + "</H1>");
        out.println(
            "<a href=\""
                + convertContextPath(GetStatusServlet.CONTEXT_PATH)
                + "\">"
                + BaseMessages.getString(PKG, "TransStatusServlet.BackToStatusPage")
                + "</a><p>");
      }
    }
  }
 public Object getKettleValue(Object input, int type) throws KettleException {
   Object result = null;
   Object valTmp = null;
   switch (type) {
     case ValueMetaInterface.TYPE_INTEGER:
       if (null == input) {
         // TODO: get default value from dialog-input
         valTmp = getDefaultValue(type);
       } else if (input instanceof Number) {
         valTmp = new Long(((Number) input).intValue());
       } else if (input instanceof Binary) {
         byte[] b = ((Binary) input).getData();
         String s = new String(b);
         valTmp = new Long(s);
       } else {
         valTmp = new Long(input.toString());
       }
       result = m_tmpValueMeta.getInteger(valTmp);
       break;
     case ValueMetaInterface.TYPE_NUMBER:
       if (null == input) {
         valTmp = getDefaultValue(type);
       } else if (input instanceof Number) {
         valTmp = new Double(((Number) input).doubleValue());
       } else if (valTmp instanceof Binary) {
         byte[] b = ((Binary) input).getData();
         String s = new String(b);
         valTmp = new Double(s);
       } else {
         valTmp = new Double(input.toString());
       }
       result = m_tmpValueMeta.getNumber(valTmp);
       break;
     case ValueMetaInterface.TYPE_STRING:
       if (null == input) {
         valTmp = getDefaultValue(type);
         result = m_tmpValueMeta.getString(valTmp);
       } else {
         result = m_tmpValueMeta.getString(input);
       }
       break;
     case ValueMetaInterface.TYPE_BINARY:
       if (null == input) {
         valTmp = getDefaultValue(type);
       } else if (input instanceof Binary) {
         valTmp = ((Binary) input).getData();
       } else {
         valTmp = input.toString().getBytes();
       }
       result = m_tmpValueMeta.getBinary(valTmp);
       break;
     case ValueMetaInterface.TYPE_BOOLEAN:
       if (null == input) {
         valTmp = getDefaultValue(type);
       } else if (input instanceof Number) {
         valTmp = new Boolean(((Number) input).intValue() != 0);
       } else if (input instanceof Date) {
         valTmp = new Boolean(((Date) input).getTime() != 0);
       } else {
         valTmp =
             new Boolean(
                 input.toString().equalsIgnoreCase("false")
                     || input.toString().equalsIgnoreCase("0"));
       }
       result = !m_tmpValueMeta.getBoolean(valTmp);
       break;
     case ValueMetaInterface.TYPE_DATE:
       if (null == input) {
         valTmp = getDefaultValue(type);
       } else if (input instanceof Number) {
         valTmp = new Date(((Number) input).longValue());
       } else if (input instanceof Date) {
         valTmp = input;
       } else {
         throw new KettleException(
             BaseMessages.getString(
                 PKG, "SequoiaDB.ErrorMessage.DateConversion", input.toString()));
       }
       result = m_tmpValueMeta.getDate(valTmp);
     default:
   }
   return result;
 }
Example #8
0
  private void writeRowToBulk(RowMetaInterface rowMeta, Object[] r) throws KettleException {

    try {
      // So, we have this output stream to which we can write CSV data to.
      // Basically, what we need to do is write the binary data (from strings to
      // it as part of this proof of concept)
      //
      // The data format required is essentially "value|value|value|value"
      // new feature implemented
      // "use SSV which requires the format to be '"value";"value","value"'
      byte[] delimiter;
      if (meta.isUseSSV()) {
        delimiter = data.semicolon;
      } else {
        delimiter = data.separator;
      }

      for (int i = 0; i < data.keynrs.length; i++) {
        if (i > 0) {
          // Write a separator
          //
          write(delimiter);
        }

        int index = data.keynrs[i];
        ValueMetaInterface valueMeta = rowMeta.getValueMeta(index);
        Object valueData = r[index];

        if (valueData == null) {
          // Don't output anything for null
          //
        } else {
          if (valueMeta.isStorageBinaryString()) {
            byte[] value = valueMeta.getBinaryString(valueData);
            write(value);
          } else {
            // We're using the bulk row metadata so dates and numerics should be in the correct
            // format now...
            //
            String string = valueMeta.getString(valueData);
            if (string != null) {
              // support of SSV feature
              //
              if (meta.isUseSSV()) {

                // replace " in string fields
                //
                if (meta.isEscapingSpecialCharacters() && valueMeta.isString()) {

                  StringBuilder builder = new StringBuilder(string);
                  String[] escapeStrings =
                      new String[] {
                        "\"", "\n", "\r",
                      };
                  String[] replaceStrings =
                      new String[] {
                        "\\\"", "\\n", "\\r",
                      };
                  for (int e = 0; e < escapeStrings.length; e++) {
                    String chr = escapeStrings[e];
                    String rep = replaceStrings[e];
                    int idx = builder.indexOf(chr, 0);
                    while (idx > 0) {
                      builder.replace(idx, idx + chr.length(), rep);
                      idx = builder.indexOf(chr, idx + rep.length());
                    }
                  }
                  string = builder.toString();
                }
                write(data.doubleQuote);
                write(data.getBytes(string));
                write(data.doubleQuote);
              } else {
                write(data.getBytes(string));
              }
            }
          }
        }
      }

      // finally write a newline
      //
      write(data.newline);
    } catch (Exception e) {
      // If something went wrong with the import,
      // rather return that error, in stead of "Pipe Broken"
      try {
        data.sqlRunner.checkExcn();
      } catch (Exception loadEx) {
        throw new KettleException("Error serializing rows of data to the fifo file", loadEx);
      }

      throw new KettleException("Error serializing rows of data to the fifo file", e);
    }
  }