Example usage for java.lang ArrayIndexOutOfBoundsException toString

List of usage examples for java.lang ArrayIndexOutOfBoundsException toString

Introduction

In this page you can find the example usage for java.lang ArrayIndexOutOfBoundsException toString.

Prototype

public String toString() 

Source Link

Document

Returns a short description of this throwable.

Usage

From source file:org.lwes.serializer.Deserializer.java

public static String deserializeSTRING(DeserializerState myState, byte[] bytes, short encoding) {
    String aString = null;/*from   www  .  ja  va2  s  . co  m*/
    int len = -1;
    try {
        len = deserializeUINT16(myState, bytes);

        //            if (log.isTraceEnabled()) {
        //                log.trace("Datagram Bytes: " +
        //                          NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
        //                log.trace("String Length: " + len);
        //                log.trace("State: " + myState);
        //            }

        aString = EncodedString.bytesToString(bytes, myState.currentIndex(), len,
                Event.ENCODING_STRINGS[encoding]);
        myState.incr(len);
    } catch (ArrayIndexOutOfBoundsException aioobe) {
        if (log.isInfoEnabled()) {
            log.info("Exception: " + aioobe.toString());
            log.info("Datagram Bytes: " + NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
            log.info("String Length: " + len);
            log.info("State: " + myState);
        }
    }
    return aString;
}

From source file:org.lwes.serializer.Deserializer.java

public static String deserializeEVENTWORD(DeserializerState myState, byte[] bytes, short encoding) {
    String aString = null;//  w ww . j  a v a2 s. c o m
    int len = -1;
    try {
        len = deserializeUBYTE(myState, bytes);

        //            if (log.isTraceEnabled()) {
        //                log.trace("Datagram Bytes: " +
        //                          NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
        //                log.trace("String Length: " + len);
        //                log.trace("State: " + myState);
        //            }

        aString = EncodedString.bytesToString(bytes, myState.currentIndex(), len,
                Event.ENCODING_STRINGS[encoding]);
        myState.incr(len);
    } catch (ArrayIndexOutOfBoundsException aioobe) {
        if (log.isInfoEnabled()) {
            log.info("Exception: " + aioobe.toString());
            log.info("Datagram Bytes: " + NumberCodec.byteArrayToHexString(bytes, 0, bytes.length));
            log.info("String Length: " + len);
            log.info("State: " + myState);
        }
    }
    return aString;
}

From source file:org.apache.ambari.view.slider.rest.client.SliderAppMasterClient.java

public Map<String, Number[][]> getMetrics(String appName, String metricsUrl, Set<String> metricsRequested,
        TemporalInfo temporalInfo, ViewContext context, SliderAppType appType, MetricsHolder metricsHolder) {
    Map<String, Number[][]> retVal = new HashMap<String, Number[][]>();

    if (appType == null || metricsHolder == null || metricsHolder.getTimelineMetrics() == null) {
        logger.info("AppType must be provided and it must contain "
                + "timeline_metrics.json to extract jmx properties");
        return retVal;
    }/* w ww  .j  a v a2s . co  m*/

    Map<String, Number[][]> receivedMetrics = null;
    List<String> components = new ArrayList<String>();
    for (SliderAppTypeComponent appTypeComponent : appType.getTypeComponents()) {
        components.add(appTypeComponent.getName());
    }

    Map<String, Map<String, Map<String, Metric>>> metrics = metricsHolder.getTimelineMetrics();
    Map<String, Metric> relevantMetrics = getRelevantMetrics(metrics, components);
    Set<String> metricsToRead = new HashSet<String>();
    Map<String, String> reverseNameLookup = new HashMap<String, String>();
    for (String key : relevantMetrics.keySet()) {
        if (metricsRequested.contains(key)) {
            String metricName = relevantMetrics.get(key).getMetric();
            metricsToRead.add(metricName);
            reverseNameLookup.put(metricName, key);
        }
    }

    if (metricsToRead.size() != 0) {
        try {
            String specWithParams = SliderAppMetricsHelper.getUrlWithParams(appName, metricsUrl, metricsToRead,
                    temporalInfo);
            logger.info("Using spec: " + specWithParams);
            if (specWithParams != null) {

                String spec = null;
                String params = null;
                String[] tokens = specWithParams.split("\\?", 2);

                try {
                    spec = tokens[0];
                    params = tokens[1];
                } catch (ArrayIndexOutOfBoundsException e) {
                    logger.info(e.toString());
                }

                receivedMetrics = SliderAppMetricsHelper.getMetrics(context, spec, params);
            }
        } catch (Exception e) {
            logger.warn("Unable to retrieve metrics. " + e.getMessage());
        }
    }

    if (receivedMetrics != null) {
        for (Map.Entry<String, Number[][]> metric : receivedMetrics.entrySet()) {
            if (reverseNameLookup.containsKey(metric.getKey())) {
                retVal.put(reverseNameLookup.get(metric.getKey()), metric.getValue());
            }
        }
    }

    return retVal;
}

From source file:be.ibridge.kettle.trans.step.excelinput.ExcelInput.java

public Row getRowFromWorkbooks() {
    // This procedure outputs a single Excel data row on the destination
    // rowsets...

    Row retval = new Row();
    retval.setIgnore();/*from   w  ww.  java 2 s . c o m*/

    try {
        // First, see if a file has been opened?
        if (data.workbook == null) {
            // Open a new workbook..
            data.file = data.files.getFile(data.filenr);
            data.filename = KettleVFS.getFilename(data.file);

            ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, data.file,
                    getTransMeta().getName(), toString());
            resultFile.setComment("File was read by an Excel input step");
            addResultFile(resultFile);

            if (log.isDetailed())
                logDetailed("Opening workbook #" + data.filenr + " : " + data.filename);
            data.workbook = Workbook.getWorkbook(data.file.getContent().getInputStream());
            data.errorHandler.handleFile(data.file);
            // Start at the first sheet again...
            data.sheetnr = 0;

        }

        boolean nextsheet = false;

        // What sheet were we handling?
        if (log.isDebug())
            logDetailed("Get sheet #" + data.filenr + "." + data.sheetnr);
        String sheetName = meta.getSheetName()[data.sheetnr];
        Sheet sheet = data.workbook.getSheet(sheetName);
        if (sheet != null) {
            // at what row do we continue reading?
            if (data.rownr < 0) {
                data.rownr = meta.getStartRow()[data.sheetnr];

                // Add an extra row if we have a header row to skip...
                if (meta.startsWithHeader()) {
                    data.rownr++;
                }
            }
            // Start at the specified column
            data.colnr = meta.getStartColumn()[data.sheetnr];

            // Build a new row and fill in the data from the sheet...
            try {
                Cell line[] = sheet.getRow(data.rownr);
                // Already increase cursor 1 row               
                int lineNr = ++data.rownr;
                // Excel starts counting at 0
                if (!data.filePlayList.isProcessingNeeded(data.file, lineNr, sheetName)) {
                    retval.setIgnore();
                } else {
                    if (log.isRowLevel())
                        logRowlevel("Get line #" + lineNr + " from sheet #" + data.filenr + "." + data.sheetnr);

                    if (log.isRowLevel())
                        logRowlevel("Read line with " + line.length + " cells");
                    ExcelInputRow excelInputRow = new ExcelInputRow(sheet.getName(), lineNr, line);
                    Row r = fillRow(data.row, data.colnr, excelInputRow);
                    if (log.isRowLevel())
                        logRowlevel("Converted line to row #" + lineNr + " : " + r);

                    boolean isEmpty = isLineEmpty(line);
                    if (!isEmpty || !meta.ignoreEmptyRows()) {
                        // Put the row
                        retval = r;
                    }

                    if (isEmpty && meta.stopOnEmpty()) {
                        nextsheet = true;
                    }
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                if (log.isRowLevel())
                    logRowlevel("Out of index error: move to next sheet!");
                // We tried to read below the last line in the sheet.
                // Go to the next sheet...
                nextsheet = true;
            }
        } else {
            nextsheet = true;
        }

        if (nextsheet) {
            // Go to the next sheet
            data.sheetnr++;

            // Reset the start-row:
            data.rownr = -1;

            // no previous row yet, don't take it from the previous sheet!
            // (that whould be plain wrong!)
            data.previousRow = null;

            // Perhaps it was the last sheet?
            if (data.sheetnr >= meta.getSheetName().length) {
                jumpToNextFile();
            }
        }
    } catch (Exception e) {
        logError("Error processing row from Excel file [" + data.filename + "] : " + e.toString());
        setErrors(1);
        stopAll();
        return null;
    }

    return retval;
}

From source file:com.panet.imeta.trans.steps.excelinput.ExcelInput.java

public Object[] getRowFromWorkbooks() {
    // This procedure outputs a single Excel data row on the destination
    // rowsets...

    Object[] retval = null;/* w ww .  j  a  va 2s.c  o m*/

    try {
        // First, see if a file has been opened?
        if (data.workbook == null) {
            // Open a new openFile..
            data.file = data.files.getFile(data.filenr);
            data.filename = KettleVFS.getFilename(data.file);
            if (meta.isAddResultFile()) {
                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, data.file,
                        getTransMeta().getName(), toString());
                resultFile.setComment(Messages.getString("ExcelInput.Log.FileReadByStep"));
                addResultFile(resultFile);
            }

            if (log.isDetailed())
                logDetailed(Messages.getString("ExcelInput.Log.OpeningFile",
                        "" + data.filenr + " : " + data.filename));

            WorkbookSettings ws = new WorkbookSettings();
            if (!Const.isEmpty(meta.getEncoding())) {
                ws.setEncoding(meta.getEncoding());
            }
            data.workbook = Workbook.getWorkbook(KettleVFS.getInputStream(data.file), ws);

            data.errorHandler.handleFile(data.file);
            // Start at the first sheet again...
            data.sheetnr = 0;

            // See if we have sheet names to retrieve, otherwise we'll have to get all sheets...
            //
            if (meta.readAllSheets()) {
                data.sheetNames = data.workbook.getSheetNames();
                data.startColumn = new int[data.sheetNames.length];
                data.startRow = new int[data.sheetNames.length];
                for (int i = 0; i < data.sheetNames.length; i++) {
                    data.startColumn[i] = data.defaultStartColumn;
                    data.startRow[i] = data.defaultStartRow;
                }
            }
        }

        boolean nextsheet = false;

        // What sheet were we handling?
        if (log.isDebug())
            logDetailed(Messages.getString("ExcelInput.Log.GetSheet", "" + data.filenr + "." + data.sheetnr));

        String sheetName = data.sheetNames[data.sheetnr];
        Sheet sheet = data.workbook.getSheet(sheetName);
        if (sheet != null) {
            // at what row do we continue reading?
            if (data.rownr < 0) {
                data.rownr = data.startRow[data.sheetnr];

                // Add an extra row if we have a header row to skip...
                if (meta.startsWithHeader()) {
                    data.rownr++;
                }
            }
            // Start at the specified column
            data.colnr = data.startColumn[data.sheetnr];

            // Build a new row and fill in the data from the sheet...
            try {
                Cell line[] = sheet.getRow(data.rownr);
                // Already increase cursor 1 row               
                int lineNr = ++data.rownr;
                // Excel starts counting at 0
                if (!data.filePlayList.isProcessingNeeded(data.file, lineNr, sheetName)) {
                    retval = null; // placeholder, was already null
                } else {
                    if (log.isRowLevel())
                        logRowlevel(Messages.getString("ExcelInput.Log.GetLine", "" + lineNr,
                                data.filenr + "." + data.sheetnr));

                    if (log.isRowLevel())
                        logRowlevel(Messages.getString("ExcelInput.Log.ReadLineWith", "" + line.length));

                    ExcelInputRow excelInputRow = new ExcelInputRow(sheet.getName(), lineNr, line);
                    Object[] r = fillRow(data.colnr, excelInputRow);
                    if (log.isRowLevel())
                        logRowlevel(Messages.getString("ExcelInput.Log.ConvertedLinToRow", "" + lineNr,
                                data.outputRowMeta.getString(r)));

                    boolean isEmpty = isLineEmpty(line);
                    if (!isEmpty || !meta.ignoreEmptyRows()) {
                        // Put the row
                        retval = r;
                    } else {
                        if (data.rownr > sheet.getRows()) {
                            nextsheet = true;
                        }
                    }

                    if (isEmpty && meta.stopOnEmpty()) {
                        nextsheet = true;
                    }
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                if (log.isRowLevel())
                    logRowlevel(Messages.getString("ExcelInput.Log.OutOfIndex"));

                // We tried to read below the last line in the sheet.
                // Go to the next sheet...
                nextsheet = true;
            }
        } else {
            nextsheet = true;
        }

        if (nextsheet) {
            // Go to the next sheet
            data.sheetnr++;

            // Reset the start-row:
            data.rownr = -1;

            // no previous row yet, don't take it from the previous sheet!
            // (that whould be plain wrong!)
            data.previousRow = null;

            // Perhaps it was the last sheet?
            if (data.sheetnr >= data.sheetNames.length) {
                jumpToNextFile();
            }
        }
    } catch (Exception e) {
        logError(Messages.getString("ExcelInput.Error.ProcessRowFromExcel", data.filename + "", e.toString()));

        setErrors(1);
        stopAll();
        return null;
    }

    return retval;
}

From source file:org.pentaho.di.trans.steps.excelinput.ExcelInput.java

public Object[] getRowFromWorkbooks() {
    // This procedure outputs a single Excel data row on the destination
    // rowsets...

    Object[] retval = null;/*  ww  w.j  a  v  a 2 s.  com*/

    try {
        // First, see if a file has been opened?
        if (data.workbook == null) {
            // Open a new openFile..
            data.file = data.files.getFile(data.filenr);
            data.filename = KettleVFS.getFilename(data.file);
            // Add additional fields?
            if (meta.getShortFileNameField() != null && meta.getShortFileNameField().length() > 0) {
                data.shortFilename = data.file.getName().getBaseName();
            }
            if (meta.getPathField() != null && meta.getPathField().length() > 0) {
                data.path = KettleVFS.getFilename(data.file.getParent());
            }
            if (meta.isHiddenField() != null && meta.isHiddenField().length() > 0) {
                data.hidden = data.file.isHidden();
            }
            if (meta.getExtensionField() != null && meta.getExtensionField().length() > 0) {
                data.extension = data.file.getName().getExtension();
            }
            if (meta.getLastModificationDateField() != null
                    && meta.getLastModificationDateField().length() > 0) {
                data.lastModificationDateTime = new Date(data.file.getContent().getLastModifiedTime());
            }
            if (meta.getUriField() != null && meta.getUriField().length() > 0) {
                data.uriName = data.file.getName().getURI();
            }
            if (meta.getRootUriField() != null && meta.getRootUriField().length() > 0) {
                data.rootUriName = data.file.getName().getRootURI();
            }
            if (meta.getSizeField() != null && meta.getSizeField().length() > 0) {
                data.size = new Long(data.file.getContent().getSize());
            }

            if (meta.isAddResultFile()) {
                ResultFile resultFile = new ResultFile(ResultFile.FILE_TYPE_GENERAL, data.file,
                        getTransMeta().getName(), toString());
                resultFile.setComment(BaseMessages.getString(PKG, "ExcelInput.Log.FileReadByStep"));
                addResultFile(resultFile);
            }

            if (log.isDetailed()) {
                logDetailed(BaseMessages.getString(PKG, "ExcelInput.Log.OpeningFile",
                        "" + data.filenr + " : " + data.filename));
            }

            data.workbook = WorkbookFactory.getWorkbook(meta.getSpreadSheetType(), data.filename,
                    meta.getEncoding());

            data.errorHandler.handleFile(data.file);
            // Start at the first sheet again...
            data.sheetnr = 0;

            // See if we have sheet names to retrieve, otherwise we'll have to get all sheets...
            //
            if (meta.readAllSheets()) {
                data.sheetNames = data.workbook.getSheetNames();
                data.startColumn = new int[data.sheetNames.length];
                data.startRow = new int[data.sheetNames.length];
                for (int i = 0; i < data.sheetNames.length; i++) {
                    data.startColumn[i] = data.defaultStartColumn;
                    data.startRow[i] = data.defaultStartRow;
                }
            }
        }

        boolean nextsheet = false;

        // What sheet were we handling?
        if (log.isDebug()) {
            logDetailed(BaseMessages.getString(PKG, "ExcelInput.Log.GetSheet",
                    "" + data.filenr + "." + data.sheetnr));
        }

        String sheetName = data.sheetNames[data.sheetnr];
        KSheet sheet = data.workbook.getSheet(sheetName);
        if (sheet != null) {
            // at what row do we continue reading?
            if (data.rownr < 0) {
                data.rownr = data.startRow[data.sheetnr];

                // Add an extra row if we have a header row to skip...
                if (meta.startsWithHeader()) {
                    data.rownr++;
                }
            }
            // Start at the specified column
            data.colnr = data.startColumn[data.sheetnr];

            // Build a new row and fill in the data from the sheet...
            try {
                KCell[] line = sheet.getRow(data.rownr);
                // Already increase cursor 1 row
                int lineNr = ++data.rownr;
                // Excel starts counting at 0
                if (!data.filePlayList.isProcessingNeeded(data.file, lineNr, sheetName)) {
                    retval = null; // placeholder, was already null
                } else {
                    if (log.isRowLevel()) {
                        logRowlevel(BaseMessages.getString(PKG, "ExcelInput.Log.GetLine", "" + lineNr,
                                data.filenr + "." + data.sheetnr));
                    }

                    if (log.isRowLevel()) {
                        logRowlevel(
                                BaseMessages.getString(PKG, "ExcelInput.Log.ReadLineWith", "" + line.length));
                    }

                    ExcelInputRow excelInputRow = new ExcelInputRow(sheet.getName(), lineNr, line);
                    Object[] r = fillRow(data.colnr, excelInputRow);
                    if (log.isRowLevel()) {
                        logRowlevel(BaseMessages.getString(PKG, "ExcelInput.Log.ConvertedLinToRow", "" + lineNr,
                                data.outputRowMeta.getString(r)));
                    }

                    boolean isEmpty = isLineEmpty(line);
                    if (!isEmpty || !meta.ignoreEmptyRows()) {
                        // Put the row
                        retval = r;
                    } else {
                        if (data.rownr > sheet.getRows()) {
                            nextsheet = true;
                        }
                    }

                    if (isEmpty && meta.stopOnEmpty()) {
                        nextsheet = true;
                    }
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                if (log.isRowLevel()) {
                    logRowlevel(BaseMessages.getString(PKG, "ExcelInput.Log.OutOfIndex"));
                }

                // We tried to read below the last line in the sheet.
                // Go to the next sheet...
                nextsheet = true;
            }
        } else {
            nextsheet = true;
        }

        if (nextsheet) {
            // Go to the next sheet
            data.sheetnr++;

            // Reset the start-row:
            data.rownr = -1;

            // no previous row yet, don't take it from the previous sheet!
            // (that whould be plain wrong!)
            data.previousRow = null;

            // Perhaps it was the last sheet?
            if (data.sheetnr >= data.sheetNames.length) {
                jumpToNextFile();
            }
        }
    } catch (Exception e) {
        logError(BaseMessages.getString(PKG, "ExcelInput.Error.ProcessRowFromExcel", data.filename + "",
                e.toString()), e);

        setErrors(1);
        stopAll();
        return null;
    }

    return retval;
}

From source file:com.panet.imeta.trans.steps.textfileinput.TextFileInput.java

public static final String[] convertLineToStrings(String line, InputFileMetaInterface inf)
        throws KettleException {
    String[] strings = new String[inf.getInputFields().length];
    int fieldnr;// w  w  w. j a  v  a 2s .  com

    String pol; // piece of line

    try {
        if (line == null)
            return null;

        if (inf.getFileType().equalsIgnoreCase("CSV")) {
            // Split string in pieces, only for CSV!

            fieldnr = 0;
            int pos = 0;
            int length = line.length();
            boolean dencl = false;

            int len_encl = (inf.getEnclosure() == null ? 0 : inf.getEnclosure().length());
            int len_esc = (inf.getEscapeCharacter() == null ? 0 : inf.getEscapeCharacter().length());

            while (pos < length) {
                int from = pos;
                int next;

                boolean encl_found;
                boolean contains_escaped_enclosures = false;
                boolean contains_escaped_separators = false;

                // Is the field beginning with an enclosure?
                // "aa;aa";123;"aaa-aaa";000;...
                if (len_encl > 0
                        && line.substring(from, from + len_encl).equalsIgnoreCase(inf.getEnclosure())) {
                    if (log.isRowLevel())
                        log.logRowlevel(Messages.getString("TextFileInput.Log.ConvertLineToRowTitle"),
                                Messages.getString("TextFileInput.Log.Encloruse",
                                        line.substring(from, from + len_encl)));
                    encl_found = true;
                    int p = from + len_encl;

                    boolean is_enclosure = len_encl > 0 && p + len_encl < length
                            && line.substring(p, p + len_encl).equalsIgnoreCase(inf.getEnclosure());
                    boolean is_escape = len_esc > 0 && p + len_esc < length
                            && line.substring(p, p + len_esc).equalsIgnoreCase(inf.getEscapeCharacter());

                    boolean enclosure_after = false;

                    // Is it really an enclosure? See if it's not repeated twice or escaped!
                    if ((is_enclosure || is_escape) && p < length - 1) {
                        String strnext = line.substring(p + len_encl, p + 2 * len_encl);
                        if (strnext.equalsIgnoreCase(inf.getEnclosure())) {
                            p++;
                            enclosure_after = true;
                            dencl = true;

                            // Remember to replace them later on!
                            if (is_escape)
                                contains_escaped_enclosures = true;
                        }
                    }

                    // Look for a closing enclosure!
                    while ((!is_enclosure || enclosure_after) && p < line.length()) {
                        p++;
                        enclosure_after = false;
                        is_enclosure = len_encl > 0 && p + len_encl < length
                                && line.substring(p, p + len_encl).equals(inf.getEnclosure());
                        is_escape = len_esc > 0 && p + len_esc < length
                                && line.substring(p, p + len_esc).equals(inf.getEscapeCharacter());

                        // Is it really an enclosure? See if it's not repeated twice or escaped!
                        if ((is_enclosure || is_escape) && p < length - 1) // Is
                        {
                            String strnext = line.substring(p + len_encl, p + 2 * len_encl);
                            if (strnext.equals(inf.getEnclosure())) {
                                p++;
                                enclosure_after = true;
                                dencl = true;

                                // Remember to replace them later on!
                                if (is_escape)
                                    contains_escaped_enclosures = true; // remember
                            }
                        }
                    }

                    if (p >= length)
                        next = p;
                    else
                        next = p + len_encl;

                    if (log.isRowLevel())
                        log.logRowlevel(Messages.getString("TextFileInput.Log.ConvertLineToRowTitle"),
                                Messages.getString("TextFileInput.Log.EndOfEnclosure", "" + p));
                } else {
                    encl_found = false;
                    boolean found = false;
                    int startpoint = from;
                    int tries = 1;
                    do {
                        next = line.indexOf(inf.getSeparator(), startpoint);

                        // See if this position is preceded by an escape character.
                        if (len_esc > 0 && next - len_esc > 0) {
                            String before = line.substring(next - len_esc, next);

                            if (inf.getEscapeCharacter().equals(before)) {
                                // take the next separator, this one is escaped...
                                startpoint = next + 1;
                                tries++;
                                contains_escaped_separators = true;
                            } else {
                                found = true;
                            }
                        } else {
                            found = true;
                        }
                    } while (!found && next >= 0);
                }
                if (next == -1)
                    next = length;

                if (encl_found && ((from + len_encl) <= (next - len_encl))) {
                    pol = line.substring(from + len_encl, next - len_encl);
                    if (log.isRowLevel())
                        log.logRowlevel(Messages.getString("TextFileInput.Log.ConvertLineToRowTitle"),
                                Messages.getString("TextFileInput.Log.EnclosureFieldFound", "" + pol));
                } else {
                    pol = line.substring(from, next);
                    if (log.isRowLevel())
                        log.logRowlevel(Messages.getString("TextFileInput.Log.ConvertLineToRowTitle"),
                                Messages.getString("TextFileInput.Log.NormalFieldFound", "" + pol));
                }

                if (dencl && Const.isEmpty(inf.getEscapeCharacter())) {
                    StringBuilder sbpol = new StringBuilder(pol);
                    int idx = sbpol.indexOf(inf.getEnclosure() + inf.getEnclosure());
                    while (idx >= 0) {
                        sbpol.delete(idx, idx + inf.getEnclosure().length());
                        idx = sbpol.indexOf(inf.getEnclosure() + inf.getEnclosure());
                    }
                    pol = sbpol.toString();
                }

                //   replace the escaped enclosures with enclosures... 
                if (contains_escaped_enclosures) {
                    String replace = inf.getEscapeCharacter() + inf.getEnclosure();
                    String replaceWith = inf.getEnclosure();

                    pol = Const.replace(pol, replace, replaceWith);
                }

                //replace the escaped separators with separators... 
                if (contains_escaped_separators) {
                    String replace = inf.getEscapeCharacter() + inf.getSeparator();
                    String replaceWith = inf.getSeparator();

                    pol = Const.replace(pol, replace, replaceWith);
                }

                // Now add pol to the strings found!
                try {
                    strings[fieldnr] = pol;
                } catch (ArrayIndexOutOfBoundsException e) {
                    // In case we didn't allocate enough space.
                    // This happens when you have less header values specified than there are actual values in the rows.
                    // As this is "the exception" we catch and resize here.
                    //
                    String[] newStrings = new String[strings.length];
                    for (int x = 0; x < strings.length; x++)
                        newStrings[x] = strings[x];
                    strings = newStrings;
                }

                pos = next + inf.getSeparator().length();
                fieldnr++;
            }
            if (pos == length) {
                if (log.isRowLevel())
                    log.logRowlevel(Messages.getString("TextFileInput.Log.ConvertLineToRowTitle"),
                            Messages.getString("TextFileInput.Log.EndOfEmptyLineFound"));
                if (fieldnr < strings.length)
                    strings[fieldnr] = Const.EMPTY_STRING;
                fieldnr++;
            }
        } else {
            // Fixed file format: Simply get the strings at the required positions...
            for (int i = 0; i < inf.getInputFields().length; i++) {
                TextFileInputField field = inf.getInputFields()[i];

                int length = line.length();

                int fieldLength = field.getLength();
                if (fieldLength < 0) {
                    fieldLength = Integer.MAX_VALUE;
                }

                if (field.getPosition() + fieldLength <= length) {
                    strings[i] = line.substring(field.getPosition(), field.getPosition() + fieldLength);
                } else {
                    if (field.getPosition() < length) {
                        strings[i] = line.substring(field.getPosition());
                    } else {
                        strings[i] = "";
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new KettleException(
                Messages.getString("TextFileInput.Log.Error.ErrorConvertingLine", e.toString()), e);
    }

    return strings;
}

From source file:org.pentaho.di.trans.steps.textfileinput.TextFileInput.java

public static final String[] convertLineToStrings(LogChannelInterface log, String line,
        InputFileMetaInterface inf, String delimiter, String enclosure, String escapeCharacters)
        throws KettleException {
    String[] strings = new String[inf.getInputFields().length];
    int fieldnr;/*  w ww.  j a  v  a  2s.com*/

    String pol; // piece of line

    try {
        if (line == null) {
            return null;
        }

        if (inf.getFileType().equalsIgnoreCase("CSV")) {
            // Split string in pieces, only for CSV!

            fieldnr = 0;
            int pos = 0;
            int length = line.length();
            boolean dencl = false;

            int len_encl = (enclosure == null ? 0 : enclosure.length());
            int len_esc = (escapeCharacters == null ? 0 : escapeCharacters.length());

            while (pos < length) {
                int from = pos;
                int next;

                boolean encl_found;
                boolean contains_escaped_enclosures = false;
                boolean contains_escaped_separators = false;

                // Is the field beginning with an enclosure?
                // "aa;aa";123;"aaa-aaa";000;...
                if (len_encl > 0 && line.substring(from, from + len_encl).equalsIgnoreCase(enclosure)) {
                    if (log.isRowLevel()) {
                        log.logRowlevel(BaseMessages.getString(PKG, "TextFileInput.Log.ConvertLineToRowTitle"),
                                BaseMessages.getString(PKG, "TextFileInput.Log.Encloruse",
                                        line.substring(from, from + len_encl)));
                    }
                    encl_found = true;
                    int p = from + len_encl;

                    boolean is_enclosure = len_encl > 0 && p + len_encl < length
                            && line.substring(p, p + len_encl).equalsIgnoreCase(enclosure);
                    boolean is_escape = len_esc > 0 && p + len_esc < length
                            && line.substring(p, p + len_esc).equalsIgnoreCase(inf.getEscapeCharacter());

                    boolean enclosure_after = false;

                    // Is it really an enclosure? See if it's not repeated twice or escaped!
                    if ((is_enclosure || is_escape) && p < length - 1) {
                        String strnext = line.substring(p + len_encl, p + 2 * len_encl);
                        if (strnext.equalsIgnoreCase(enclosure)) {
                            p++;
                            enclosure_after = true;
                            dencl = true;

                            // Remember to replace them later on!
                            if (is_escape) {
                                contains_escaped_enclosures = true;
                            }
                        }
                    }

                    // Look for a closing enclosure!
                    while ((!is_enclosure || enclosure_after) && p < line.length()) {
                        p++;
                        enclosure_after = false;
                        is_enclosure = len_encl > 0 && p + len_encl < length
                                && line.substring(p, p + len_encl).equals(enclosure);
                        is_escape = len_esc > 0 && p + len_esc < length
                                && line.substring(p, p + len_esc).equals(inf.getEscapeCharacter());

                        // Is it really an enclosure? See if it's not repeated twice or escaped!
                        if ((is_enclosure || is_escape) && p < length - 1) {

                            String strnext = line.substring(p + len_encl, p + 2 * len_encl);
                            if (strnext.equals(enclosure)) {
                                p++;
                                enclosure_after = true;
                                dencl = true;

                                // Remember to replace them later on!
                                if (is_escape) {
                                    contains_escaped_enclosures = true; // remember
                                }
                            }
                        }
                    }

                    if (p >= length) {
                        next = p;
                    } else {
                        next = p + len_encl;
                    }

                    if (log.isRowLevel()) {
                        log.logRowlevel(BaseMessages.getString(PKG, "TextFileInput.Log.ConvertLineToRowTitle"),
                                BaseMessages.getString(PKG, "TextFileInput.Log.EndOfEnclosure", "" + p));
                    }
                } else {
                    encl_found = false;
                    boolean found = false;
                    int startpoint = from;
                    // int tries = 1;
                    do {
                        next = line.indexOf(delimiter, startpoint);

                        // See if this position is preceded by an escape character.
                        if (len_esc > 0 && next - len_esc > 0) {
                            String before = line.substring(next - len_esc, next);

                            if (inf.getEscapeCharacter().equals(before)) {
                                // take the next separator, this one is escaped...
                                startpoint = next + 1;
                                // tries++;
                                contains_escaped_separators = true;
                            } else {
                                found = true;
                            }
                        } else {
                            found = true;
                        }
                    } while (!found && next >= 0);
                }
                if (next == -1) {
                    next = length;
                }

                if (encl_found && ((from + len_encl) <= (next - len_encl))) {
                    pol = line.substring(from + len_encl, next - len_encl);
                    if (log.isRowLevel()) {
                        log.logRowlevel(BaseMessages.getString(PKG, "TextFileInput.Log.ConvertLineToRowTitle"),
                                BaseMessages.getString(PKG, "TextFileInput.Log.EnclosureFieldFound", "" + pol));
                    }
                } else {
                    pol = line.substring(from, next);
                    if (log.isRowLevel()) {
                        log.logRowlevel(BaseMessages.getString(PKG, "TextFileInput.Log.ConvertLineToRowTitle"),
                                BaseMessages.getString(PKG, "TextFileInput.Log.NormalFieldFound", "" + pol));
                    }
                }

                if (dencl && Const.isEmpty(inf.getEscapeCharacter())) {
                    StringBuilder sbpol = new StringBuilder(pol);
                    int idx = sbpol.indexOf(enclosure + enclosure);
                    while (idx >= 0) {
                        sbpol.delete(idx, idx + enclosure.length());
                        idx = sbpol.indexOf(enclosure + enclosure);
                    }
                    pol = sbpol.toString();
                }

                // replace the escaped enclosures with enclosures...
                if (contains_escaped_enclosures) {
                    String replace = inf.getEscapeCharacter() + enclosure;
                    String replaceWith = enclosure;

                    pol = Const.replace(pol, replace, replaceWith);
                }

                // replace the escaped separators with separators...
                if (contains_escaped_separators) {
                    String replace = inf.getEscapeCharacter() + delimiter;
                    String replaceWith = delimiter;

                    pol = Const.replace(pol, replace, replaceWith);
                }

                // Now add pol to the strings found!
                try {
                    strings[fieldnr] = pol;
                } catch (ArrayIndexOutOfBoundsException e) {
                    // In case we didn't allocate enough space.
                    // This happens when you have less header values specified than there are actual values in the rows.
                    // As this is "the exception" we catch and resize here.
                    //
                    String[] newStrings = new String[strings.length];
                    for (int x = 0; x < strings.length; x++) {
                        newStrings[x] = strings[x];
                    }
                    strings = newStrings;
                }

                pos = next + delimiter.length();
                fieldnr++;
            }
            if (pos == length) {
                if (log.isRowLevel()) {
                    log.logRowlevel(BaseMessages.getString(PKG, "TextFileInput.Log.ConvertLineToRowTitle"),
                            BaseMessages.getString(PKG, "TextFileInput.Log.EndOfEmptyLineFound"));
                }
                if (fieldnr < strings.length) {
                    strings[fieldnr] = Const.EMPTY_STRING;
                }
                fieldnr++;
            }
        } else {
            // Fixed file format: Simply get the strings at the required positions...
            for (int i = 0; i < inf.getInputFields().length; i++) {
                TextFileInputField field = inf.getInputFields()[i];

                int length = line.length();

                if (field.getPosition() + field.getLength() <= length) {
                    strings[i] = line.substring(field.getPosition(), field.getPosition() + field.getLength());
                } else {
                    if (field.getPosition() < length) {
                        strings[i] = line.substring(field.getPosition());
                    } else {
                        strings[i] = "";
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new KettleException(
                BaseMessages.getString(PKG, "TextFileInput.Log.Error.ErrorConvertingLine", e.toString()), e);
    }

    return strings;
}

From source file:org.pentaho.di.trans.steps.fileinput.text.TextFileInputUtils.java

public static final String[] convertLineToStrings(LogChannelInterface log, String line, TextFileInputMeta inf,
        String delimiter, String enclosure, String escapeCharacters) throws KettleException {
    String[] strings = new String[inf.inputFields.length];
    int fieldnr;/*  www . j av  a2 s  .c  o  m*/

    String pol; // piece of line

    try {
        if (line == null) {
            return null;
        }

        if (inf.content.fileType.equalsIgnoreCase("CSV")) {
            // Split string in pieces, only for CSV!

            fieldnr = 0;
            int pos = 0;
            int length = line.length();
            boolean dencl = false;

            int len_encl = (enclosure == null ? 0 : enclosure.length());
            int len_esc = (escapeCharacters == null ? 0 : escapeCharacters.length());

            while (pos < length) {
                int from = pos;
                int next;

                boolean encl_found;
                boolean contains_escaped_enclosures = false;
                boolean contains_escaped_separators = false;
                boolean contains_escaped_escape = false;

                // Is the field beginning with an enclosure?
                // "aa;aa";123;"aaa-aaa";000;...
                if (len_encl > 0 && line.substring(from, from + len_encl).equalsIgnoreCase(enclosure)) {
                    if (log.isRowLevel()) {
                        log.logRowlevel(BaseMessages.getString(PKG, "TextFileInput.Log.ConvertLineToRowTitle"),
                                BaseMessages.getString(PKG, "TextFileInput.Log.Encloruse",
                                        line.substring(from, from + len_encl)));
                    }
                    encl_found = true;
                    int p = from + len_encl;

                    boolean is_enclosure = len_encl > 0 && p + len_encl < length
                            && line.substring(p, p + len_encl).equalsIgnoreCase(enclosure);
                    boolean is_escape = len_esc > 0 && p + len_esc < length
                            && line.substring(p, p + len_esc).equalsIgnoreCase(inf.content.escapeCharacter);

                    boolean enclosure_after = false;

                    // Is it really an enclosure? See if it's not repeated twice or escaped!
                    if ((is_enclosure || is_escape) && p < length - 1) {
                        String strnext = line.substring(p + len_encl, p + 2 * len_encl);
                        if (strnext.equalsIgnoreCase(enclosure)) {
                            p++;
                            enclosure_after = true;
                            dencl = true;

                            // Remember to replace them later on!
                            if (is_escape) {
                                contains_escaped_enclosures = true;
                            }
                        } else if (strnext.equals(inf.content.escapeCharacter)) {
                            p++;
                            // Remember to replace them later on!
                            if (is_escape) {
                                contains_escaped_escape = true; // remember
                            }
                        }
                    }

                    // Look for a closing enclosure!
                    while ((!is_enclosure || enclosure_after) && p < line.length()) {
                        p++;
                        enclosure_after = false;
                        is_enclosure = len_encl > 0 && p + len_encl < length
                                && line.substring(p, p + len_encl).equals(enclosure);
                        is_escape = len_esc > 0 && p + len_esc < length
                                && line.substring(p, p + len_esc).equals(inf.content.escapeCharacter);

                        // Is it really an enclosure? See if it's not repeated twice or escaped!
                        if ((is_enclosure || is_escape) && p < length - 1) {

                            String strnext = line.substring(p + len_encl, p + 2 * len_encl);
                            if (strnext.equals(enclosure)) {
                                p++;
                                enclosure_after = true;
                                dencl = true;

                                // Remember to replace them later on!
                                if (is_escape) {
                                    contains_escaped_enclosures = true; // remember
                                }
                            } else if (strnext.equals(inf.content.escapeCharacter)) {
                                p++;
                                // Remember to replace them later on!
                                if (is_escape) {
                                    contains_escaped_escape = true; // remember
                                }
                            }
                        }
                    }

                    if (p >= length) {
                        next = p;
                    } else {
                        next = p + len_encl;
                    }

                    if (log.isRowLevel()) {
                        log.logRowlevel(BaseMessages.getString(PKG, "TextFileInput.Log.ConvertLineToRowTitle"),
                                BaseMessages.getString(PKG, "TextFileInput.Log.EndOfEnclosure", "" + p));
                    }
                } else {
                    encl_found = false;
                    boolean found = false;
                    int startpoint = from;
                    // int tries = 1;
                    do {
                        next = line.indexOf(delimiter, startpoint);

                        // See if this position is preceded by an escape character.
                        if (len_esc > 0 && next > 0) {
                            String before = line.substring(next - len_esc, next);

                            if (inf.content.escapeCharacter.equals(before)) {
                                int previous_escapes = 1;

                                int start = next - len_esc - 1;
                                int end = next - 1;

                                while (start >= 0) {
                                    if (inf.content.escapeCharacter.equals(line.substring(start, end))) {
                                        previous_escapes++;
                                        start--;
                                        end--;
                                    } else {
                                        break;
                                    }
                                }

                                // If behind the seperator there are a odd number of escaped
                                // The separator is escaped.
                                if (previous_escapes % 2 != 0) {
                                    // take the next separator, this one is escaped...
                                    startpoint = next + 1;
                                    // tries++;
                                    contains_escaped_separators = true;
                                } else {
                                    found = true;
                                }

                            } else {
                                found = true;
                            }
                        } else {
                            found = true;
                        }
                    } while (!found && next >= 0);
                }
                if (next == -1) {
                    next = length;
                }

                if (encl_found && ((from + len_encl) <= (next - len_encl))) {
                    pol = line.substring(from + len_encl, next - len_encl);
                    if (log.isRowLevel()) {
                        log.logRowlevel(BaseMessages.getString(PKG, "TextFileInput.Log.ConvertLineToRowTitle"),
                                BaseMessages.getString(PKG, "TextFileInput.Log.EnclosureFieldFound", "" + pol));
                    }
                } else {
                    pol = line.substring(from, next);
                    if (log.isRowLevel()) {
                        log.logRowlevel(BaseMessages.getString(PKG, "TextFileInput.Log.ConvertLineToRowTitle"),
                                BaseMessages.getString(PKG, "TextFileInput.Log.NormalFieldFound", "" + pol));
                    }
                }

                if (dencl && Utils.isEmpty(inf.content.escapeCharacter)) {
                    StringBuilder sbpol = new StringBuilder(pol);
                    int idx = sbpol.indexOf(enclosure + enclosure);
                    while (idx >= 0) {
                        sbpol.delete(idx, idx + enclosure.length());
                        idx = sbpol.indexOf(enclosure + enclosure);
                    }
                    pol = sbpol.toString();
                }

                // replace the escaped enclosures with enclosures...
                if (contains_escaped_enclosures) {
                    String replace = inf.content.escapeCharacter + enclosure;
                    String replaceWith = enclosure;

                    pol = Const.replace(pol, replace, replaceWith);
                }

                // replace the escaped separators with separators...
                if (contains_escaped_separators) {
                    String replace = inf.content.escapeCharacter + delimiter;
                    String replaceWith = delimiter;

                    pol = Const.replace(pol, replace, replaceWith);
                }

                // replace the escaped escape with escape...
                contains_escaped_escape = pol
                        .contains(inf.content.escapeCharacter + inf.content.escapeCharacter);
                if (contains_escaped_escape) {
                    String replace = inf.content.escapeCharacter + inf.content.escapeCharacter;
                    String replaceWith = inf.content.escapeCharacter;

                    pol = Const.replace(pol, replace, replaceWith);
                }

                // Now add pol to the strings found!
                try {
                    strings[fieldnr] = pol;
                } catch (ArrayIndexOutOfBoundsException e) {
                    // In case we didn't allocate enough space.
                    // This happens when you have less header values specified than there are actual values in the rows.
                    // As this is "the exception" we catch and resize here.
                    //
                    String[] newStrings = new String[strings.length];
                    for (int x = 0; x < strings.length; x++) {
                        newStrings[x] = strings[x];
                    }
                    strings = newStrings;
                }

                pos = next + delimiter.length();
                fieldnr++;
            }
            if (pos == length) {
                if (log.isRowLevel()) {
                    log.logRowlevel(BaseMessages.getString(PKG, "TextFileInput.Log.ConvertLineToRowTitle"),
                            BaseMessages.getString(PKG, "TextFileInput.Log.EndOfEmptyLineFound"));
                }
                if (fieldnr < strings.length) {
                    strings[fieldnr] = Const.EMPTY_STRING;
                }
                fieldnr++;
            }
        } else {
            // Fixed file format: Simply get the strings at the required positions...
            // Note - charBased is the old default behavior. If this is an old transformation, content.length will be null
            // and should be processed as before. If the content.length is equal to "Characters" or there is no specified encoding,
            // it will still use the old behavior. The *only* way to get the new behavior is if content.length = "Bytes" and
            // the encoding is specified.
            boolean charBased = (inf.content.length == null || inf.content.length.equalsIgnoreCase("Characters")
                    || inf.getEncoding() == null); // Default to classic behavior
            for (int i = 0; i < inf.inputFields.length; i++) {
                BaseFileField field = inf.inputFields[i];

                int length;
                int fPos = field.getPosition();
                int fLength = field.getLength();
                int fPl = fPos + fLength;
                if (charBased) {
                    length = line.length();
                    if (fPl <= length) {
                        strings[i] = line.substring(fPos, fPl);
                    } else {
                        if (fPos < length) {
                            strings[i] = line.substring(fPos);
                        } else {
                            strings[i] = "";
                        }
                    }
                } else {
                    byte[] b = null;
                    String enc = inf.getEncoding();
                    b = line.getBytes(enc);
                    length = b.length;
                    if (fPl <= length) {
                        strings[i] = new String(Arrays.copyOfRange(b, fPos, fPl), enc);
                    } else {
                        if (fPos < length) {
                            strings[i] = new String(Arrays.copyOfRange(b, fPos, length - 1), enc);
                        } else {
                            strings[i] = "";
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new KettleException(
                BaseMessages.getString(PKG, "TextFileInput.Log.Error.ErrorConvertingLine", e.toString()), e);
    }

    return strings;
}

From source file:org.lockss.servlet.HashCUS.java

private boolean checkParams() {
    auid = getParameter(KEY_AUID);//from   w w  w  .  j a va  2 s  .  co  m
    url = getParameter(KEY_URL);
    lower = getParameter(KEY_LOWER);
    upper = getParameter(KEY_UPPER);
    isRecord = (getParameter(KEY_RECORD) != null);
    alg = req.getParameter(KEY_ALG);
    if (StringUtil.isNullString(alg)) {
        alg = LcapMessage.getDefaultHashAlgorithm();
    }
    String hTypeStr = getParameter(KEY_HASH_TYPE);
    if (StringUtil.isNullString(hTypeStr)) {
        hType = DEFAULT_HASH_TYPE;
    } else if (StringUtils.isNumeric(hTypeStr)) {
        try {
            int hTypeInt = Integer.parseInt(hTypeStr);
            hType = hashTypeCompat[hTypeInt];
            if (hType == null)
                throw new ArrayIndexOutOfBoundsException();
        } catch (ArrayIndexOutOfBoundsException e) {
            errMsg = "Unknown hash type: " + hTypeStr;
            return false;
        } catch (RuntimeException e) {
            errMsg = "Can't parse hash type: " + hTypeStr;
            return false;
        }
    } else {
        try {
            hType = HashType.valueOf(hTypeStr);
        } catch (IllegalArgumentException e) {
            errMsg = "Unknown hash type: " + hTypeStr;
            return false;
        }
    }
    String resTypeStr = getParameter(KEY_RESULT_TYPE);
    if (StringUtil.isNullString(resTypeStr)) {
        resType = DEFAULT_RESULT_TYPE;
    } else {
        try {
            resType = ResultType.valueOf(resTypeStr);
        } catch (IllegalArgumentException e) {
            errMsg = "Unknown result type: " + resTypeStr;
            return false;
        }
    }
    String resEncodingStr = getParameter(KEY_RESULT_ENCODING);
    if (StringUtil.isNullString(resEncodingStr)) {
        resEncoding = DEFAULT_RESULT_ENCODING;
    } else {
        try {
            resEncoding = ResultEncoding.valueOf(resEncodingStr);
        } catch (IllegalArgumentException e) {
            errMsg = "Unknown result encoding: " + resEncodingStr;
            return false;
        }
    }
    if (auid == null) {
        errMsg = "Select an AU";
        return false;
    }
    au = pluginMgr.getAuFromId(auid);
    if (au == null) {
        errMsg = "No such AU.  Select an AU";
        return false;
    }
    if (url == null) {
        url = AuCachedUrlSetSpec.URL;
        //       errMsg = "URL required";
        //       return false;
    }
    try {
        challenge = getB64Param(KEY_CHALLENGE);
    } catch (IllegalArgumentException e) {
        errMsg = "Challenge: Illegal Base64 string: " + e.getMessage();
        return false;
    }
    try {
        verifier = getB64Param(KEY_VERIFIER);
    } catch (IllegalArgumentException e) {
        errMsg = "Verifier: Illegal Base64 string: " + e.getMessage();
        return false;
    }
    PollSpec ps;
    try {
        switch (hType) {
        case V1File:
            if (upper != null || (lower != null && !lower.equals(PollSpec.SINGLE_NODE_LWRBOUND))) {
                errMsg = "Upper/Lower ignored";
            }
            ps = new PollSpec(auid, url, PollSpec.SINGLE_NODE_LWRBOUND, null, Poll.V1_CONTENT_POLL);
            break;
        case V3Tree:

            ps = new PollSpec(auid, url, lower, upper, Poll.V3_POLL);
            break;
        case V3File:
            ps = new PollSpec(auid, url, PollSpec.SINGLE_NODE_LWRBOUND, null, Poll.V3_POLL);
            break;
        default:
            ps = new PollSpec(auid, url, lower, upper, Poll.V1_CONTENT_POLL);
        }
    } catch (Exception e) {
        errMsg = "Error making PollSpec: " + e.toString();
        log.debug("Making Pollspec", e);
        return false;
    }
    log.debug("" + ps);
    cus = ps.getCachedUrlSet();
    if (cus == null) {
        errMsg = "No such CUS: " + ps;
        return false;
    }
    log.debug("" + cus);
    return true;
}