Example usage for java.lang StringBuffer replace

List of usage examples for java.lang StringBuffer replace

Introduction

In this page you can find the example usage for java.lang StringBuffer replace.

Prototype

@Override
public synchronized StringBuffer replace(int start, int end, String str) 

Source Link

Usage

From source file:org.pentaho.di.job.entries.dtdvalidator.DTDValidator.java

public boolean validate() {

    boolean retval = false;

    FileObject xmlfile = null;/*w ww  .j  av a2 s  . c  om*/
    FileObject DTDfile = null;

    ByteArrayInputStream ba = null;
    try {
        if (xmlfilename != null && ((getDTDFilename() != null && !isInternDTD()) || (isInternDTD()))) {
            xmlfile = KettleVFS.getFileObject(getXMLFilename());

            if (xmlfile.exists()) {

                URL xmlFile = new File(KettleVFS.getFilename(xmlfile)).toURI().toURL();
                StringBuffer xmlStringbuffer = new StringBuffer("");

                BufferedReader xmlBufferedReader = null;
                InputStreamReader is = null;
                try {
                    // open XML File
                    is = new InputStreamReader(xmlFile.openStream());
                    xmlBufferedReader = new BufferedReader(is);

                    char[] buffertXML = new char[1024];
                    int LenXML = -1;
                    while ((LenXML = xmlBufferedReader.read(buffertXML)) != -1) {
                        xmlStringbuffer.append(buffertXML, 0, LenXML);
                    }
                } finally {
                    if (is != null) {
                        is.close();
                    }
                    if (xmlBufferedReader != null) {
                        xmlBufferedReader.close();
                    }
                }

                // Prepare parsing ...
                DocumentBuilderFactory DocBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder DocBuilder = DocBuilderFactory.newDocumentBuilder();

                // Let's try to get XML document encoding

                DocBuilderFactory.setValidating(false);
                ba = new ByteArrayInputStream(xmlStringbuffer.toString().getBytes("UTF-8"));
                Document xmlDocDTD = DocBuilder.parse(ba);
                if (ba != null) {
                    ba.close();
                }

                String encoding = null;
                if (xmlDocDTD.getXmlEncoding() == null) {
                    encoding = "UTF-8";
                } else {
                    encoding = xmlDocDTD.getXmlEncoding();
                }

                int xmlStartDTD = xmlStringbuffer.indexOf("<!DOCTYPE");

                if (isInternDTD()) {
                    // DTD find in the XML document
                    if (xmlStartDTD != -1) {
                        log.logBasic(BaseMessages.getString(PKG, "JobEntryDTDValidator.ERRORDTDFound.Label",
                                getXMLFilename()));
                    } else {
                        setErrorMessage(BaseMessages.getString(PKG,
                                "JobEntryDTDValidator.ERRORDTDNotFound.Label", getXMLFilename()));
                    }

                } else {
                    // DTD in external document
                    // If we find an intern declaration, we remove it
                    DTDfile = KettleVFS.getFileObject(getDTDFilename());

                    if (DTDfile.exists()) {
                        if (xmlStartDTD != -1) {
                            int EndDTD = xmlStringbuffer.indexOf(">", xmlStartDTD);
                            // String DocTypeDTD = xmlStringbuffer.substring(xmlStartDTD, EndDTD + 1);
                            xmlStringbuffer.replace(xmlStartDTD, EndDTD + 1, "");
                        }

                        String xmlRootnodeDTD = xmlDocDTD.getDocumentElement().getNodeName();

                        String RefDTD = "<?xml version='" + xmlDocDTD.getXmlVersion() + "' encoding='"
                                + encoding + "'?>\n<!DOCTYPE " + xmlRootnodeDTD + " SYSTEM '"
                                + KettleVFS.getFilename(DTDfile) + "'>\n";

                        int xmloffsetDTD = xmlStringbuffer.indexOf("<" + xmlRootnodeDTD);
                        xmlStringbuffer.replace(0, xmloffsetDTD, RefDTD);
                    } else {
                        log.logError(
                                BaseMessages.getString(PKG,
                                        "JobEntryDTDValidator.ERRORDTDFileNotExists.Subject"),
                                BaseMessages.getString(PKG, "JobEntryDTDValidator.ERRORDTDFileNotExists.Msg",
                                        getDTDFilename()));
                    }
                }

                if (!(isInternDTD() && xmlStartDTD == -1 || (!isInternDTD() && !DTDfile.exists()))) {

                    // Let's parse now ...
                    MyErrorHandler error = new MyErrorHandler();
                    DocBuilderFactory.setValidating(true);
                    DocBuilder = DocBuilderFactory.newDocumentBuilder();
                    DocBuilder.setErrorHandler(error);

                    ba = new ByteArrayInputStream(xmlStringbuffer.toString().getBytes(encoding));
                    xmlDocDTD = DocBuilder.parse(ba);

                    if (error.errorMessage == null) {
                        log.logBasic(BaseMessages.getString(PKG, "JobEntryDTDValidator.DTDValidatorOK.Subject"),
                                BaseMessages.getString(PKG, "JobEntryDTDValidator.DTDValidatorOK.Label",
                                        getXMLFilename()));

                        // Everything is OK
                        retval = true;
                    } else {
                        // Invalid DTD
                        setNrErrors(error.nrErrors);
                        setErrorMessage(BaseMessages.getString(PKG, "JobEntryDTDValidator.DTDValidatorKO",
                                getXMLFilename(), error.nrErrors, error.errorMessage));
                    }
                }

            } else {
                if (!xmlfile.exists()) {
                    setErrorMessage(BaseMessages.getString(PKG, "JobEntryDTDValidator.FileDoesNotExist.Label",
                            getXMLFilename()));
                }
            }
        } else {
            setErrorMessage(BaseMessages.getString(PKG, "JobEntryDTDValidator.AllFilesNotNull.Label"));
        }
    } catch (Exception e) {
        setErrorMessage(BaseMessages.getString(PKG, "JobEntryDTDValidator.ErrorDTDValidator.Label",
                getXMLFilename(), getDTDFilename(), e.getMessage()));
    } finally {
        try {
            if (xmlfile != null) {
                xmlfile.close();
            }
            if (DTDfile != null) {
                DTDfile.close();
            }
            if (ba != null) {
                ba.close();
            }
        } catch (IOException e) {
            // Ignore close errors
        }
    }
    return retval;
}

From source file:cn.jsprun.struts.action.BasicSettingsAction.java

private boolean validateExpressions(String expression) {
    String reg = "[\\*/\\+]";
    String reg2 = "\\-";
    String reg3 = "[\\-]?[\\(]*[\\-]?extcredits[1-8][\\)]*";
    String reg4 = "\\(";
    String reg5 = "[\\-]?[\\(]*[\\-]?[0-9]+[\\)]*";
    String reg5_ = "[\\-]?[\\(]*[\\-]?[0-9]*.[0-9]+[\\)]*";
    String reg6 = "[\\-]?[\\(]*[\\-]?posts[\\)]*";
    String reg7 = "[\\-]?[\\(]*[\\-]?digestposts[\\)]*";
    String reg8 = "[\\-]?[\\(]*[\\-]?oltime[\\)]*";
    String reg9 = "[\\-]?[\\(]*[\\-]?pageviews[\\)]*";
    String[] temp = expression.split(reg);
    for (int i = 0; i < temp.length; i++) {
        if (temp[i].trim().equals("") || temp[i].trim().endsWith("-")) {
            return false;
        }//from  w  w  w.j  a  v  a 2s  .c o  m
    }
    for (int i = 0; i < temp.length; i++) {
        String[] temp2 = temp[i].trim().split(reg2);
        int sign = 0;
        for (int j = 0; j < temp2.length; j++) {
            String temp2v = temp2[j].trim();
            if ((!temp2v.matches(reg3) && !temp2v.matches(reg4) && !temp2v.matches(reg5)
                    && !temp2v.matches(reg5_) && !temp2v.matches(reg6) && !temp2v.matches(reg7)
                    && !temp2v.matches(reg8) && !temp2v.matches(reg9)) && !temp2v.equals("")) {
                return false;
            }
            if (sign == 2) {
                return false;
            }
            if (temp2v.equals("")) {
                sign++;
            }
            if (sign == 1 && !temp2v.equals("")) {
                sign--;
            }
        }
    }
    boolean boolFront = true;
    StringBuffer buffer = new StringBuffer(expression);
    int chrIndex = 0;
    for (int i = 0; i < buffer.length(); i++) {
        char chr = buffer.charAt(i);
        if (boolFront && chr == ')') {
            return false;
        }
        if (chr == '(') {
            chrIndex = i;
            boolFront = false;
        }
        if (!boolFront && chr == ')') {
            buffer.replace(chrIndex, chrIndex + 1, "V");
            buffer.replace(i, i + 1, "V");
            boolFront = true;
            i = -1;
        }
    }
    if (!boolFront) {
        return false;
    }
    return true;
}

From source file:Repackage.java

public void repackageJavaFile(String name) throws IOException {
    File sourceFile = new File(_sourceBase, name);
    StringBuffer sb = readFile(sourceFile);

    Matcher packageMatcher = _packagePattern.matcher(sb);

    if (packageMatcher.find()) {
        String pkg = packageMatcher.group(1);
        int pkgStart = packageMatcher.start(1);
        int pkgEnd = packageMatcher.end(1);

        if (packageMatcher.find())
            throw new RuntimeException("Two package specifications found: " + name);

        List filePath = Repackager.splitPath(name, File.separatorChar);
        String srcDir = Repackager.dirForPath(name);

        // Sort the repackage spec so that longer from's are first to match
        // longest package first

        for (;;) {
            boolean swapped = false;

            for (int i = 1; i < filePath.size(); i++) {
                String spec1 = (String) filePath.get(i - 1);
                String spec2 = (String) filePath.get(i);

                if (spec1.indexOf(':') < spec2.indexOf(':')) {
                    filePath.set(i - 1, spec2);
                    filePath.set(i, spec1);

                    swapped = true;//w w w. j a  v  a 2  s.  c  o  m
                }
            }

            if (!swapped)
                break;
        }

        List pkgPath = Repackager.splitPath(pkg, '.');

        int f = filePath.size() - 2;

        if (f < 0 || (filePath.size() - 1) < pkgPath.size())
            throw new RuntimeException("Package spec differs from file path: " + name);

        for (int i = pkgPath.size() - 1; i >= 0; i--) {
            if (!pkgPath.get(i).equals(filePath.get(f)))
                throw new RuntimeException("Package spec differs from file path: " + name);
            f--;
        }

        List changeTo = null;
        List changeFrom = null;

        from: for (int i = 0; i < _fromPackages.size(); i++) {
            List from = (List) _fromPackages.get(i);

            if (from.size() <= pkgPath.size()) {
                for (int j = 0; j < from.size(); j++)
                    if (!from.get(j).equals(pkgPath.get(j)))
                        continue from;

                changeFrom = from;
                changeTo = (List) _toPackages.get(i);

                break;
            }
        }

        if (changeTo != null) {
            String newPkg = "";
            String newName = "";

            for (int i = 0; i < changeTo.size(); i++) {
                if (i > 0) {
                    newPkg += ".";
                    newName += File.separatorChar;
                }

                newPkg += changeTo.get(i);
                newName += changeTo.get(i);
            }

            for (int i = filePath.size() - pkgPath.size() - 2; i >= 0; i--)
                newName = (String) filePath.get(i) + File.separatorChar + newName;

            for (int i = changeFrom.size(); i < pkgPath.size(); i++) {
                newName += File.separatorChar + (String) pkgPath.get(i);
                newPkg += '.' + (String) pkgPath.get(i);
            }

            newName += File.separatorChar + (String) filePath.get(filePath.size() - 1);

            sb.replace(pkgStart, pkgEnd, newPkg);

            name = newName;
            String newDir = Repackager.dirForPath(name);

            if (!srcDir.equals(newDir)) {
                _movedDirs.put(srcDir, newDir);
            }
        }
    }

    File targetFile = new File(_targetBase, name); // new name

    if (sourceFile.lastModified() < targetFile.lastModified()) {
        _skippedFiles += 1;
        return;
    }

    writeFile(new File(_targetBase, name), _repackager.repackage(sb));
}

From source file:com.edgenius.wiki.render.filter.MacroFilter.java

public String filter(String input, final RenderContext context) {

    final List<Region> pairRegion = new ArrayList<Region>();

    String result = singleMacroProvider.replaceByTokenVisitor(input, new TokenVisitor<Matcher>() {
        public void handleMatch(StringBuffer buffer, Matcher matcher) {
            handleMacro(false, buffer, matcher.toMatchResult(), context, pairRegion, null);
        }/*from   ww w.j  ava2s .  c  o  m*/

    });

    // scenarios (s = start, u = unknown - no any attributes in macro, so could be start or end):
    // A B B A - B is process by recursive inside A
    // A B A B - B is treat as invalid - ignore
    // Au Au Au Au - First 2 Au is paired, Last 2 Au is paired
    // As1 As2 Au2 Au1 - OK, this case is special, which can not process by original Paired Regex patter
    //                because it will treat it as As1 and Au2 as paired, but ignore As2 and Au1. 
    //               Here just try to resolve this problem. 
    //exceptions
    // As1 As2 Au1 - then As1 can not find pair - no processed , but As2 could match with Au1

    int size = pairRegion.size();
    if (size > 0) {
        StringBuffer inputBuf = new StringBuffer(result);
        for (int idx = 0; idx < size; idx++) {
            Region reg = pairRegion.get(idx);
            int deep = 0;
            Region pair = null;
            //looking for pairs...
            for (int chIdx = idx + 1; chIdx < size; chIdx++) {
                Region next = pairRegion.get(chIdx);
                if (StringUtils.equalsIgnoreCase(reg.getContent(), next.getContent())) {
                    //start is unknown (no attribute), then end must be unknown
                    if (MACRO_REGION_KEY_UNKNOWN.equals(reg.getKey())
                            && MACRO_REGION_KEY_UNKNOWN.equals(next.getKey())) {
                        //matched
                        pair = next;
                        //skip all internal node - which is handle by embedded recursive
                        idx = chIdx;
                        break;
                    }

                    if (MACRO_REGION_KEY_START.equals(reg.getKey())
                            && MACRO_REGION_KEY_UNKNOWN.equals(next.getKey())) {
                        if (deep == 0) {
                            //matched;
                            pair = next;
                            //skip all internal node - which is handle by embedded recursive
                            idx = chIdx;
                            break;
                        } else {
                            //just another inner same name macro matched, deep minus  
                            deep--;
                        }
                    }
                    if (MACRO_REGION_KEY_START.equals(next.getKey())) {
                        //ok, it gets another start, in 4th scenarios - then add deep
                        deep++;
                    }
                }
            }
            //ok, success find paired
            if (pair != null) {
                CharSequence macroContent = inputBuf.subSequence(reg.getStart(), pair.getEnd());
                //for example, {font:size=12}abc{font}, the value is start markup string,i.e., {font:size=12}
                //so far, this text is useful to display markup if it has errors - which need highlight and with hover text 
                final String pairStartMarkup = pair.getBody();
                result = pairedMacroProvider.replaceByTokenVisitor(macroContent, new TokenVisitor<Matcher>() {
                    public void handleMatch(StringBuffer buffer, Matcher matcher) {
                        handleMacro(true, buffer, matcher.toMatchResult(), context, null, pairStartMarkup);
                    }
                });
                reg.setBody(result);
                reg.setSubRegion(pair);
                reg.setKey(MACRO_REGION_KEY_PROCESSED);
            }
        }

        //reverse, and replace input by region processed string (region.getBody())
        for (int idx = size - 1; idx >= 0; idx--) {
            Region reg = pairRegion.get(idx);
            if (!MACRO_REGION_KEY_PROCESSED.equals(reg.getKey()))
                continue;

            inputBuf.replace(reg.getStart(), reg.getSubRegion().getEnd(), reg.getBody());
        }

        return inputBuf.toString();
    }

    return result;
}

From source file:tokyo.northside.jrst.JRSTLexer.java

/**
 * read table simple and complexe/*from  w  w w .j  a  v  a  2s  .  c o m*/
 * 
 * <pre>
 * +------------------------+------------+----------+----------+
 * | Header row, column 1   | Header 2   | Header 3 | Header 4 |
 * | (header rows optional) |            |          |          |
 * +========================+============+==========+==========+
 * | body row 1, column 1   | column 2   | column 3 | column 4 |
 * +------------------------+------------+----------+----------+
 * | body row 2             | Cells may span columns.          |
 * +------------------------+------------+---------------------+
 * </pre>
 * 
 * @return Element
 * @throws IOException
 */
@SuppressWarnings("unchecked")
public Element peekTable() throws IOException {
    beginPeek();

    Element result = null;
    // in.skipBlankLines();
    String line = in.readLine();

    if (line != null) {
        Pattern pTableBegin = Pattern.compile("^\\s*(\\+-+)+\\+\\s*$");
        Matcher matcher = null;

        matcher = pTableBegin.matcher(line);
        if (matcher.matches()) { // complexe table
            result = DocumentHelper.createElement(TABLE);
            result.addAttribute(TABLE_HEADER, FALSE);
            int level = level(line);
            result.addAttribute(LEVEL, String.valueOf(level));
            line = line.trim();
            int tableWidth = line.length();
            result.addAttribute(TABLE_WIDTH, String.valueOf(tableWidth));

            Pattern pCellEnd = Pattern
                    .compile("^\\s{" + level + "}(\\+-+\\+|\\|(?:[^+]+))([^+]+(?:\\+|\\|\\s*$)|-+\\+)*\\s*"); // fin
            // de
            // ligne
            Pattern pCell = Pattern.compile("^\\s{" + level + "}(\\|[^|]+)+\\|\\s*$"); // une ligne
            Pattern pHeader = Pattern.compile("^\\s{" + level + "}(\\+=+)+\\+\\s*$"); // fin du header
            Pattern pEnd = Pattern.compile("^\\s{" + level + "}(\\+-+)+\\+\\s*$"); // fin de table

            // used to know if | is cell separator or not
            String lastSeparationLine = line;
            String lastLine = line;

            Element row = DocumentHelper.createElement(ROW);
            String[] table = in.readUntilBlank();

            boolean done = false;
            for (String l : table) {
                done = false;
                l = l.trim();
                if (l.length() != tableWidth) {
                    // Erreur dans la table, peut-etre lever une exception ?
                    result = null;
                    break;
                }
                matcher = pEnd.matcher(l);
                if (!done && matcher.matches()) {
                    // fin normale de ligne, on peut directement l'assigner
                    lastSeparationLine = l;
                    for (Element cell : (List<Element>) row.elements()) {
                        cell.addAttribute(CELL_END, TRUE);
                    }
                    row.addAttribute(ROW_END_HEADER, FALSE);
                    result.add(row);
                    row = DocumentHelper.createElement(ROW);
                    done = true;
                }
                matcher = pHeader.matcher(l);
                if (!done && matcher.matches()) {
                    // fin de du header, on peut directement l'assigner
                    lastSeparationLine = l;
                    for (Element cell : (List<Element>) row.elements()) {
                        cell.addAttribute(CELL_END, TRUE);
                    }
                    row.addAttribute(ROW_END_HEADER, TRUE);
                    result.add(row);
                    result.addAttribute(TABLE_HEADER, TRUE);
                    row = DocumentHelper.createElement(ROW);
                    done = true;
                }
                matcher = pCell.matcher(l);
                if (!done && matcher.matches()) {
                    // debug
                    row.addAttribute("debug", "pCell");
                    // recuperation des textes des cellules
                    int start = -1;
                    String content = "";
                    matcher = Pattern.compile("([^|]+)\\|").matcher(l);
                    for (int cellNumber = 0; matcher.find(); cellNumber++) {
                        int tmpstart = matcher.start(1);
                        int end = matcher.end(1);
                        String tmpcontent = matcher.group(1);
                        // on a forcement un | ou un + au dessus du +
                        // et forcement un + sur lastSeparationLine
                        // sinon ca veut dire qu'il y avait un | dans la
                        // cell
                        if ((lastLine.charAt(end) == '|' || lastLine.charAt(end) == '+')
                                && lastSeparationLine.charAt(end) == '+') {
                            if ("".equals(content)) {
                                content = tmpcontent;
                            } else {
                                content += tmpcontent;
                            }
                            if (start == -1) {
                                start = tmpstart;
                            }
                            Element cell = null;
                            if (row.nodeCount() <= cellNumber) {
                                cell = row.addElement(CELL);
                                cell.addAttribute(CELL_END, FALSE);
                            } else {
                                cell = (Element) row.node(cellNumber);
                            }
                            cell.addAttribute(CELL_INDEX_START, String.valueOf(start));
                            cell.addAttribute(CELL_INDEX_END, String.valueOf(end));
                            cell.setText(cell.getText() + content + "\n");
                            start = end + 1; // +1 to pass + or | at end
                            // of cell
                            content = "";
                        } else {
                            // start = tmpstart;
                            if (start == -1) {
                                start = tmpstart;
                            }
                            content += tmpcontent + "|";
                            cellNumber--;
                        }
                    }
                    done = true;
                }
                matcher = pCellEnd.matcher(l);
                if (!done && matcher.matches()) {
                    // debug
                    row.addAttribute("debug", "pCellEnd");
                    // fin d'une ligne, on ne peut pas l'assigner
                    // directement
                    // pour chaque continuation de cellule, il faut copier
                    // l'ancienne valeur

                    // mais on commence tout de meme par fermer tout les
                    // cells
                    for (Element cell : (List<Element>) row.elements()) {
                        cell.addAttribute(CELL_END, TRUE);
                    }

                    StringBuffer tmp = new StringBuffer(l);
                    int start = -1;
                    String content = "";
                    matcher = Pattern.compile("([^+|]+|-+)([+|])").matcher(l);
                    for (int cellNumber = 0; matcher.find(); cellNumber++) {
                        int tmpstart = matcher.start(1);
                        int end = matcher.end(1);
                        String tmpcontent = matcher.group(1);
                        String ender = matcher.group(2);
                        if (!tmpcontent.matches("-+")) {
                            // on a forcement un | au dessus du + ou du |
                            // sinon ca veut dire qu'il y avait un + dans la
                            // cell
                            if (lastLine.charAt(end) == '|') {
                                if (start == -1) {
                                    start = tmpstart;
                                }
                                // -1 and +1 to take the + or | at begin and
                                // end
                                String old = lastSeparationLine.substring(start - 1, end + 1);
                                tmp.replace(start - 1, end + 1, old);
                                if ("".equals(content)) {
                                    content = tmpcontent;
                                }
                                Element cell = null;
                                if (row.nodeCount() <= cellNumber) {
                                    cell = row.addElement(CELL);
                                } else {
                                    cell = (Element) row.node(cellNumber);

                                }
                                cell.setText(cell.getText() + content + "\n");
                                // on a ajouter des choses dans la cell,
                                // donc
                                // ce n'est pas la fin
                                cell.addAttribute(CELL_END, FALSE);
                                cell.addAttribute(CELL_INDEX_START, String.valueOf(start));
                                cell.addAttribute(CELL_INDEX_END, String.valueOf(end));
                                start = end + 1; // +1 to pass + or | at
                                // end of cell
                                content = "";
                            } else {
                                // start = tmpstart;
                                content += tmpcontent + ender;
                            }
                        }
                    }
                    lastSeparationLine = tmp.toString();
                    row.addAttribute(ROW_END_HEADER, FALSE);
                    result.add(row);
                    row = DocumentHelper.createElement(ROW);
                    done = true;
                }
                if (!done) {
                    log.warn("Bad table format line " + in.getLineNumber());
                }
                lastLine = l;
            }

            //
            // line += "\n" + joinBlock(table, "\n", false);
            //
            // result.addText(line);
        } else if (line.matches("^\\s*(=+ +)+=+\\s*$")) {
            // Les donnees de la table peuvent depasser de celle-ci
            /*
             * ===== ===== ====== Inputs Output ------------ ------ A B A or
             * B ===== ===== ====== False False Second column of row 1. True
             * False Second column of row 2.
             * 
             * True 2 - Second column of row 3.
             * 
             * - Second item in bullet list (row 3, column 2). ============
             * ======
             */

            result = DocumentHelper.createElement(TABLE);
            line = line.trim();
            Pattern pBordersEquals = Pattern.compile("^\\s*(=+ +)+=+\\s*$"); // Separation
            // =
            Pattern pBordersTiret = Pattern.compile("^\\s*(-+ +)+-+\\s*$"); // Separation
            // -
            Pattern pBorders = Pattern.compile("^\\s*([=-]+ +)+[=-]+\\s*$"); // =
            // ou
            // -
            String[] table = in.readUntilBlank(); // Recuperation de la
            // table

            int tableWidth = line.length();
            int nbSeparations = 0;
            for (String l : table) {
                if (l.length() > tableWidth) {
                    tableWidth = l.length(); // Determination de la
                } // Determination de la
                  // longueur max
                matcher = pBordersEquals.matcher(l);
                if (matcher.matches()) {
                    nbSeparations++;
                }

            }
            // Header if the table contains 3 equals separations
            result.addAttribute(TABLE_HEADER, "" + (nbSeparations == 2));
            int level = level(line);
            result.addAttribute(LEVEL, String.valueOf(level));
            result.addAttribute(TABLE_WIDTH, String.valueOf(tableWidth + 1));
            Element row = DocumentHelper.createElement(ROW);
            // Determination of the columns positions
            List<Integer> columns = new LinkedList<Integer>();
            matcher = Pattern.compile("=+\\s+").matcher(line);
            for (int cellNumber = 0; matcher.find(); cellNumber++) {
                columns.add(matcher.end());
            }
            columns.add(tableWidth);

            // Traitement du tbl
            /*
             * ===== ===== ====== Inputs Output ------------ ------ A B A or
             * B ===== ===== ====== False False Second column of row 1. True
             * False Second column of row 2.
             * 
             * True 2 - Second column of row 3.
             * 
             * - Second item in bullet list (row 3, column 2). ============
             * ====== devient l'equivalent : ===== ===== ====== Inputs
             * Output ------------ ------ A B A or B ===== ===== ======
             * False False Second column of row 1. ----- ----- ------ True
             * False Second column of row 2. ----- ----- ------ True 2 -
             * Second column of row 3. - Second item in bullet list (row 3,
             * column 2). ============ ======
             */
            String lineRef = line.replace('=', '-');
            Matcher matcher2;
            List<String> tableTmp = new LinkedList<String>();

            for (int i = 0; i < table.length - 1; i++) {
                tableTmp.add(table[i]);
                if (!table[i].equals("")) {
                    if (!table[i + 1].substring(0, columns.get(0)).matches("\\s*")) {
                        matcher = pBorders.matcher(table[i]);
                        matcher2 = pBorders.matcher(table[i + 1]);
                        if (!matcher.matches() && !matcher2.matches() && !table[i + 1].equals("")) {
                            tableTmp.add(lineRef);
                        }
                    }
                }
            }
            tableTmp.add(table[table.length - 1]);
            table = new String[tableTmp.size()];
            for (int i = 0; i < tableTmp.size(); i++) {
                table[i] = tableTmp.get(i);
            }

            boolean done = false;
            LinkedList<String> lastLines = new LinkedList<String>();
            int separation = 1;
            for (String l : table) {
                if (l != null) {
                    done = false;
                    matcher = pBordersTiret.matcher(l);
                    matcher2 = pBordersEquals.matcher(l);
                    if (matcher.matches() || matcher2.matches()) { // Intermediate
                        // separation
                        while (!lastLines.isEmpty()) {
                            matcher = Pattern.compile("[-=]+\\s*").matcher(l);
                            String tmpLine = lastLines.getLast();
                            lastLines.removeLast();
                            int cellNumber;
                            for (cellNumber = 0; matcher.find(); cellNumber++) {
                                Element cell = null;
                                if (row.nodeCount() <= cellNumber) {
                                    cell = row.addElement(CELL);
                                } else {
                                    cell = (Element) row.node(cellNumber);
                                }
                                if (matcher.start() < tmpLine.length()) {
                                    if (columns.size() - 1 == cellNumber) {
                                        cell.setText(
                                                tmpLine.substring(matcher.start(), tmpLine.length()) + "\n");
                                    } else {
                                        if (matcher.end() < tmpLine.length()) {
                                            cell.setText(
                                                    tmpLine.substring(matcher.start(), matcher.end()) + "\n");
                                        } else {
                                            cell.setText(tmpLine.substring(matcher.start(), tmpLine.length())
                                                    + "\n");
                                        }
                                    }
                                }

                                if (lastLines.size() == 0) {
                                    row.addAttribute("debug", "pCell");
                                    cell.addAttribute(CELL_END, TRUE);
                                } else {
                                    row.addAttribute("debug", "pCellEnd");
                                    cell.addAttribute(CELL_END, FALSE);
                                }
                                cell.addAttribute(CELL_INDEX_START, String.valueOf(matcher.start() + 1));
                                if (line.length() == matcher.end()) {
                                    cell.addAttribute(CELL_INDEX_END,
                                            String.valueOf(columns.get(columns.size() - 1)));
                                } else {
                                    cell.addAttribute(CELL_INDEX_END, String.valueOf(matcher.end()));
                                }
                            }

                            if (matcher2.matches()) {
                                separation++;
                                row.addAttribute(ROW_END_HEADER, "" + (separation == 2));
                            } else {
                                row.addAttribute(ROW_END_HEADER, FALSE);
                            }

                            result.add(row);
                            row = DocumentHelper.createElement(ROW);
                            done = true;
                        }
                    }
                    if (!done && l.matches("^\\s*(.+ +)+.+\\s*$")) {
                        // Data
                        lastLines.addFirst(l); // Les donnees sont stoquee
                        // dans une file d'attente
                        // lastLines (FIFO)
                        done = true;
                    }
                    if (!done) {
                        log.warn("Bad table format line " + in.getLineNumber());
                    }
                }
            }
        }
    }
    endPeek();

    return result;
}

From source file:com.topsec.tsm.sim.report.web.TopoReportController.java

/**
 *  ? ?//from   ww  w  .  j  av a 2 s  .  c om
 * @param sid
 * @param request
 * @param response
 * @return
 * @throws Exception
 */
public String comprehensiveInformReport(SID sid, HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    JSONObject json = new JSONObject();
    ReportBean bean = new ReportBean();
    bean = ReportUiUtil.tidyFormBean(bean, request);
    String[] talCategory = bean.getTalCategory();
    ReportModel.setBeanPropery(bean);
    RptMasterTbService rptMasterTbImp = (RptMasterTbService) SpringContextServlet.springCtx
            .getBean(ReportUiConfig.MstBean);
    List<Map<String, Object>> comprehensiveSubAndColList = new ArrayList<Map<String, Object>>();
    List<String> hasComprehensiveReport = reportService.findAllTypeList();
    IsEditNodeType isEditNodeType = nodeTypeService.findIsEditByUserName(sid.getUserName());
    if (GlobalUtil.isNullOrEmpty(isEditNodeType)) {
        isEditNodeType = new IsEditNodeType();
        initEditNodeType(isEditNodeType, sid);
        if (!GlobalUtil.isNullOrEmpty(isEditNodeType.getUserName())) {
            nodeTypeService.saveIsEditNodeType(isEditNodeType);
        }
    }
    List<String> dvcTypes = null;
    String scanNodeId = null;
    List<SimDatasource> simDatasources = dataSourceService.getDataSource(DataSourceService.CMD_ALL);
    if (!isEditNodeType.getIsEdit()) {
        if (!GlobalUtil.isNullOrEmpty(simDatasources)) {
            dvcTypes = setDvcTypes(sid, simDatasources, bean, dvcTypes);
            hasComprehensive(dvcTypes, hasComprehensiveReport);
        }
        saveAndUpDateStatus(dvcTypes, sid, isEditNodeType);
    } else {
        List<String> deviceTypeList = ReportModel.getDeviceTypeList(dataSourceService, sid);
        hasComprehensive(deviceTypeList, hasComprehensiveReport);
        if (!GlobalUtil.isNullOrEmpty(deviceTypeList)) {
            List<NodeTypeShow> nodeTypeShowList = nodeTypeService.findTypeShowsByUserName(sid.getUserName());
            removeShowsType(deviceTypeList, nodeTypeShowList);
            saveAndUpDateStatus(deviceTypeList, sid, isEditNodeType);
            if (!GlobalUtil.isNullOrEmpty(nodeTypeShowList)) {
                for (NodeTypeShow nodeTypeShow : nodeTypeShowList) {
                    nodeTypeService.deleteNodeTypeShow(nodeTypeShow);
                }
            }
        }

        List<NodeTypeShow> nodeTypeShows = nodeTypeService.findByUserNameAndIsShow(sid.getUserName(), true);
        dvcTypes = new ArrayList<String>(nodeTypeShows.size());
        if (!GlobalUtil.isNullOrEmpty(nodeTypeShows)) {
            for (NodeTypeShow nodeTypeShow : nodeTypeShows) {
                if (!dvcTypes.contains(nodeTypeShow.getNodeType())) {
                    dvcTypes.add(nodeTypeShow.getNodeType());
                }
            }
        }
    }
    if (!GlobalUtil.isNullOrEmpty(simDatasources)) {
        scanNodeId = simDatasources.get(0).getAuditorNodeId();
    } else {
        scanNodeId = "";
    }
    List<String> mstrptidAndNodeTypeList = new ArrayList<String>();
    setMstIdAndScanNodeType(dvcTypes, mstrptidAndNodeTypeList);
    int evtRptsize = 0;
    if (!GlobalUtil.isNullOrEmpty(mstrptidAndNodeTypeList)) {
        for (String string : mstrptidAndNodeTypeList) {
            List<Map<String, Object>> subResult = rptMasterTbImp.queryTmpList(ReportUiConfig.MstSubSql,
                    new Object[] { StringUtil.toInt((string.split("IDandNODEtype"))[0],
                            StringUtil.toInt(bean.getTalTop(), 5)) });
            Map<Integer, Integer> rowColumns = ReportModel.getRowColumns(subResult);
            if (!GlobalUtil.isNullOrEmpty(subResult)) {
                for (Map map2 : subResult) {
                    map2.put("subject", (string.split("IDandNODEtype"))[1]);
                }
                Map subResultAndRowColsMap = new HashMap<String, Object>();
                subResultAndRowColsMap.put("subResult", subResult);
                subResultAndRowColsMap.put("rowColumns", rowColumns);
                subResultAndRowColsMap.put("scanNodeType", string.split("IDandNODEtype")[1]);
                comprehensiveSubAndColList.add(subResultAndRowColsMap);
            }

        }
    }

    Map<String, Object> params = new HashMap<String, Object>();
    params.put("eTime", bean.getTalEndTime());
    String sUrl = null;
    int screenWidth = StringUtil.toInt(request.getParameter("screenWidth"), 1280) - 25 - 200;

    StringBuffer subUrl = new StringBuffer();
    StringBuffer htmlOut = new StringBuffer();
    Map layoutValue = new HashMap();
    htmlOut.append("<table><tbody>");
    int tdnum = 0;
    boolean repeatRep = false;
    List<Object> subIdList = new ArrayList<Object>();
    int totalSub = 0;
    for (Map<String, Object> map : comprehensiveSubAndColList) {
        List<Map> subResult = (List<Map>) map.get("subResult");
        int len = subResult.size();
        totalSub += len;
        for (int i = 0; i < len; i++) {
            Map subMap = subResult.get(i);
            Object subId = subMap.get("subId");
            if (!subIdList.contains(subId)) {
                subIdList.add(subId);
            }
        }
    }
    if (subIdList.size() < totalSub) {
        repeatRep = true;
    }
    int repeatj = 0;
    for (Map<String, Object> map : comprehensiveSubAndColList) {
        List<Map> subResult = (List<Map>) map.get("subResult");
        Map<Integer, Integer> rowColumns = (Map<Integer, Integer>) map.get("rowColumns");
        String scanNodeType = (String) map.get("scanNodeType");
        StringBuffer layout = new StringBuffer();
        int divPaneltotalwidth = 0;
        divPaneltotalwidth = (int) ((screenWidth - 10) / 2.3);
        for (int i = 0, len = subResult.size(); i < len; i++) {
            params.remove("sTime");
            Map subMap = subResult.get(i);
            if (i == 0) {
                bean.setViewItem(StringUtil.toString(subMap.get("viewItem"), ""));
            }
            Integer row = (Integer) subMap.get("subRow");
            layout.append(row + ":" + subMap.get("subColumn") + ",");
            if (GlobalUtil.isNullOrEmpty(subMap)) {
                continue;
            }
            params.put("sTime", bean.getTalStartTime());
            params.put("dvcType", subMap.get("subject"));
            params.put("onlyTable", "onlyTable");
            String reportType = request.getParameter("reportType");
            if (!GlobalUtil.isNullOrEmpty(reportType)) {
                params.put("reportType", reportType);
            }
            sUrl = getComprehensiveUrl(ReportUiConfig.subEvtUrl, scanNodeId, params, bean.getTalCategory())
                    .toString();
            params.remove("dvcType");
            subUrl.replace(0, subUrl.length(), sUrl);
            subUrl.append("&").append(ReportUiConfig.subrptid).append("=").append(subMap.get("subId"));
            subUrl.substring(0, subUrl.length());
            if (repeatRep) {
                subUrl.append("subREPEAT_").append(repeatj++);
            }
            int column = rowColumns.get(row);
            String width = String.valueOf(((screenWidth - 10 * column) / column) / 2.4);
            String _column = subMap.get("subColumn").toString();
            subMap.put("InformReportOnlyTable", true);
            layoutValue.put(row + _column, ReportUiUtil.createSubTitle(subMap, width, subUrl.toString(),
                    bean.getTalCategory(), StringUtil.toInt(bean.getTalTop(), 5)));
        }
        String htmlLayoutmp = ReportModel.createMstTable(layout.toString(), layoutValue);
        String cnDvcName = ReportUiUtil.getDeviceTypeName(scanNodeType.replace("Comprehensive", ""),
                Locale.getDefault());
        if (tdnum % 2 == 0) {
            htmlOut.append("<tr>");
        }
        tdnum++;
        htmlOut.append("<td style='padding: 14px'><div id='div_tt' title='" + cnDvcName)
                .append("' class='easyui-panel' data-options='headerCls:\"sim-panel-header\",width:"
                        + divPaneltotalwidth + ",closable:true'")
                .append(htmlLayoutmp).append("</div></td>");
        if (tdnum % 2 == 0) {
            htmlOut.append("</tr>");
        }
    }
    if (comprehensiveSubAndColList.size() > 0) {
        request.setAttribute("title", "??");
    }
    String htmlLayout = htmlOut.toString();
    StringBuffer sb = getExportUrl(request, params, talCategory, true);
    request.setAttribute("expUrl", sb.toString());
    request.setAttribute("layout", htmlLayout);
    request.setAttribute("bean", bean);

    return "/page/report/assetStatusEvtReport";
}

From source file:com.krawler.common.notification.handlers.NotificationExtractorManager.java

public void replacePlaceholders(StringBuffer htmlMsg, NotificationRequest notificationReqObj,
        User recipientUser, DateFormat df) {
    try {//from  w  w  w  . j a  v  a2 s.  c o  m
        // e.g 
        // First Type of placeholder => #reftype1:oppmainowner.firstName#
        // Second Type of placeholder => #reftype1:oppname#
        java.lang.reflect.Method objMethod;
        String expr = "[#]{1}[a-zA-Z0-9]+[:]{1}[a-zA-Z0-9]+(\\.){0,1}[a-zA-Z0-9]*[#]{1}";
        Pattern p = Pattern.compile(expr);
        Matcher m = p.matcher(htmlMsg);
        while (m.find()) {
            String table = m.group();
            String woHash = table.substring(1, table.length() - 1);
            String[] sp = woHash.split(":");
            if (!StringUtil.isNullOrEmpty(sp[0])) { // sp[0] having reftype1 which holds placeholder class path
                Class cl = notificationReqObj.getClass();
                String methodStr = sp[0].substring(0, 1).toUpperCase() + sp[0].substring(1); // Make first letter of operand capital.
                String methodGetIdStr = methodStr.replace("type", "id");
                objMethod = cl.getMethod("get" + methodStr + ""); // Gets the value of the operand
                String classPath = (String) objMethod.invoke(notificationReqObj);
                objMethod = cl.getMethod("get" + methodGetIdStr + ""); //refid1 which holds placeholder class object primary id
                String classObjectId = (String) objMethod.invoke(notificationReqObj);

                // Placeholder Class
                String value = "-";
                Object invoker = commonTablesDAOObj.getObject(classPath, classObjectId); // load placeholder class
                Class placeHolderClass = invoker.getClass();
                String[] operator = sp[1].split("\\.");
                // 
                if (operator.length > 1) { // if having oppmainowner.firstName
                    methodStr = operator[0].substring(0, 1).toUpperCase() + operator[0].substring(1); // Make first letter of operand capital.
                    objMethod = placeHolderClass.getMethod("get" + methodStr + "");
                    Object innerClassObject = objMethod.invoke(invoker); // get oppmainowner object
                    if (!StringUtil.isNullObject(innerClassObject)) {
                        placeHolderClass = innerClassObject.getClass();
                        methodStr = operator[1].substring(0, 1).toUpperCase() + operator[1].substring(1); // Make first letter of operand capital.
                        objMethod = placeHolderClass.getMethod("get" + methodStr + "");// get oppmainowner's firstName field
                        value = String.valueOf(objMethod.invoke(innerClassObject));
                    }
                } else if (operator.length == 1) { // if having oppname
                    methodStr = operator[0].substring(0, 1).toUpperCase() + operator[0].substring(1); // Make first letter of operand capital.
                    objMethod = placeHolderClass.getMethod("get" + methodStr + "");
                    java.util.Date.class.isAssignableFrom(objMethod.getReturnType());
                    if (java.util.Date.class.isAssignableFrom(objMethod.getReturnType()))
                        value = df.format(((java.util.Date) objMethod.invoke(invoker)));
                    else if ((methodStr.equals("Startdate")
                            && java.lang.Long.class.isAssignableFrom(objMethod.getReturnType()))
                            || (methodStr.equals("Enddate")
                                    && java.lang.Long.class.isAssignableFrom(objMethod.getReturnType()))) {
                        value = df.format(new java.util.Date((java.lang.Long) objMethod.invoke(invoker)));
                    } else
                        value = String.valueOf(objMethod.invoke(invoker));
                } else {
                    value = table.replaceAll("#", "@~@~");
                }

                int i1 = htmlMsg.indexOf(table);
                int i2 = htmlMsg.indexOf(table) + table.length();
                if (StringUtil.isNullOrEmpty(value)) {
                    value = "";
                }
                if (i1 >= 0) {
                    htmlMsg.replace(i1, i2, value);
                }
            }
            m = p.matcher(htmlMsg);
        }

        // replace receiver placeholders
        expr = "[$]{1}recipient[:]{1}[a-zA-Z0-9]+(\\.){0,1}[a-zA-Z0-9]*[$]{1}"; //$recipient:firstName$ $recipient:lastName$
        p = Pattern.compile(expr);
        m = p.matcher(htmlMsg);
        while (m.find()) {
            String table = m.group();
            String woHash = table.substring(1, table.length() - 1);
            String[] sp = woHash.split(":");
            String value = "-";
            if (!StringUtil.isNullOrEmpty(sp[0])) { // sp[0] having recipient which holds placeholder class path
                Class placeHolderClass = recipientUser.getClass();
                String[] operator = sp[1].split("\\.");
                if (operator.length > 1) { // if having oppmainowner.firstName
                    String methodStr = operator[0].substring(0, 1).toUpperCase() + operator[0].substring(1); // Make first letter of operand capital.
                    objMethod = placeHolderClass.getMethod("get" + methodStr + "");
                    Object innerClassObject = objMethod.invoke(recipientUser); // get oppmainowner object
                    if (!StringUtil.isNullObject(innerClassObject)) {
                        placeHolderClass = innerClassObject.getClass();
                        methodStr = operator[1].substring(0, 1).toUpperCase() + operator[1].substring(1); // Make first letter of operand capital.
                        objMethod = placeHolderClass.getMethod("get" + methodStr + "");// get oppmainowner's firstName field
                        value = (String) objMethod.invoke(innerClassObject);
                    }
                } else if (operator.length == 1) { // if having oppname
                    String methodStr = operator[0].substring(0, 1).toUpperCase() + operator[0].substring(1); // Make first letter of operand capital.
                    objMethod = placeHolderClass.getMethod("get" + methodStr + "");
                    java.util.Date.class.isAssignableFrom(objMethod.getReturnType());
                    if (java.util.Date.class.isAssignableFrom(objMethod.getReturnType()))
                        value = df.format(((java.util.Date) objMethod.invoke(recipientUser)));
                    else
                        value = (String) objMethod.invoke(recipientUser);
                } else {
                    value = table.replaceAll("$", "@~@~");
                }

                int i1 = htmlMsg.indexOf(table);
                int i2 = htmlMsg.indexOf(table) + table.length();
                if (StringUtil.isNullOrEmpty(value)) {
                    value = "";
                }
                if (i1 >= 0) {
                    htmlMsg.replace(i1, i2, value);
                }
            }
            m = p.matcher(htmlMsg);
        }
    } catch (IllegalAccessException ex) {
        LOG.info(ex.getMessage(), ex);
        ex.printStackTrace();
    } catch (IllegalArgumentException ex) {
        LOG.info(ex.getMessage(), ex);
        ex.printStackTrace();
    } catch (InvocationTargetException ex) {
        LOG.info(ex.getMessage(), ex);
        ex.printStackTrace();
    } catch (NoSuchMethodException ex) {
        LOG.info(ex.getMessage(), ex);
        ex.printStackTrace();
    } catch (ServiceException ex) {
        LOG.info(ex.getMessage(), ex);
        ex.printStackTrace();
    }

}

From source file:org.safs.selenium.util.JavaScriptFunctions.java

/**
 * Compare 2 javascript objects.<br>
 * /*from   ww w. j  a v a  2  s  .  co m*/
 * <pre>
 *  StringBuffer jsScript = new StringBuffer();
 *  List<String> properties = new List<String>();
 *  properties.add("value");
 *  properties.add("id");
 *  properties.add("name");
 *  jsScript.append(JavaScriptFunctions.compareObject(properties));
 *  //object1 and object2 are javascript objects.
 *  jsScript.append("return compareObject(object1, object2);");
 *  boolean equaled = WDLibrary.executeScript(jsScript.toString());
 * </pre>
 * 
 * <br><b>depending on: nothing.</b><br>
 * <br><b>depending level: 0</b><br>
 * 
 * @param properties List, the property names to get value to compare between 2 objects.
 * @param object1 (<b>Javascript</b>) Object, the javascript object to compare
 * @param object2 (<b>Javascript</b>) Object, the javascript object to compare
 */
public static String compareObject(List<String> properties) {
    StringBuffer scriptCommand = new StringBuffer();

    scriptCommand.append("function compareObject(obj1, obj2){\n");
    scriptCommand.append("  equaled = false;\n");
    scriptCommand.append("  try{\n");
    scriptCommand.append("    if(obj1==obj2) equaled=true;\n");
    scriptCommand.append("    if(!equaled){\n");
    //compare each property of the 2 objects.
    if (!properties.isEmpty()) {
        scriptCommand.append("      equaled=(");
        for (String property : properties) {
            scriptCommand.append("obj1." + property + "==obj2." + property + " &&");
        }
        int lastAndSignIndex = scriptCommand.lastIndexOf("&&");
        scriptCommand.replace(lastAndSignIndex, scriptCommand.length(), ");\n");
    }
    scriptCommand.append("    }\n");
    scriptCommand.append("  }catch(error){}\n");
    scriptCommand.append("  return equaled;\n");
    scriptCommand.append("}\n");
    return scriptCommand.toString();
}

From source file:com.google.enterprise.connector.sharepoint.spiimpl.SharepointConnectorType.java

/**
 * Validates all the patterns under included / excluded to check if any of
 * them is not a valid pattern.// ww w .j av  a 2s . c  o m
 *
 * @param patterns
 *          The pattern to be validated
 * @return the set of wrong patterns, if any. Otherwise returns null
 */
private Set<String> validatePatterns(final String patterns) {
    LOGGER.info("validating patterns [ " + patterns + " ]. ");
    String[] patternsList = null;
    if ((patterns != null) && (patterns.trim().length() != 0)) {
        patternsList = patterns.split(SPConstants.SEPARATOR);
    }
    if (patternsList == null) {
        return null;
    }

    final Set<String> invalidPatterns = new HashSet<String>();

    for (final String pattern : patternsList) {
        if (pattern.startsWith(SPConstants.HASH) || pattern.startsWith(SPConstants.MINUS)) {
            continue;
        }
        if (pattern.startsWith(SPConstants.CONTAINS)) {
            final StringBuffer tempBuffer = new StringBuffer(pattern);
            if (tempBuffer == null) {
                invalidPatterns.add(pattern);
            }
            final String strContainKey = new String(tempBuffer.delete(0, SPConstants.CONTAINS.length()));
            try {
                new RE(strContainKey); // with case
            } catch (final Exception e) {
                invalidPatterns.add(pattern);
            }
            continue;
        }

        if (pattern.startsWith(SPConstants.REGEXP)) {
            final StringBuffer tempBuffer = new StringBuffer(pattern);
            if (tempBuffer == null) {
                invalidPatterns.add(pattern);
            }
            final String strRegexPattrn = new String(tempBuffer.delete(0, SPConstants.REGEXP.length()));
            try {
                new RE(strRegexPattrn);
            } catch (final Exception e) {
                invalidPatterns.add(pattern);
            }
            continue;
        }

        if (pattern.startsWith(SPConstants.REGEXP_CASE)) {
            final StringBuffer tempBuffer = new StringBuffer(pattern);
            if (tempBuffer == null) {
                invalidPatterns.add(pattern);
            }
            final String strRegexCasePattrn = new String(
                    tempBuffer.delete(0, SPConstants.REGEXP_CASE.length()));
            try {
                new RE(strRegexCasePattrn);
            } catch (final Exception e) {
                invalidPatterns.add(pattern);
            }
            continue;
        }

        if (pattern.startsWith(SPConstants.REGEXP_IGNORE_CASE)) {
            final StringBuffer tempBuffer = new StringBuffer(pattern);
            if (tempBuffer == null) {
                invalidPatterns.add(pattern);
            }
            final String strRegexIgnoreCasePattrn = new String(
                    tempBuffer.delete(0, SPConstants.REGEXP_IGNORE_CASE.length()));
            try {
                new RE(strRegexIgnoreCasePattrn, RE.REG_ICASE); // ignore
                // case
            } catch (final Exception e) {
                invalidPatterns.add(pattern);
            }
            continue;
        }

        if (pattern.startsWith(SPConstants.CARET) || pattern.endsWith(SPConstants.DOLLAR)) {
            StringBuffer tempBuffer = new StringBuffer(pattern);
            boolean bDollar = false;
            if (pattern.startsWith(SPConstants.CARET)) {
                tempBuffer = new StringBuffer(pattern);
                final int indexOfStar = tempBuffer.indexOf("*");
                if (indexOfStar != -1) {
                    tempBuffer.replace(indexOfStar, indexOfStar + "*".length(), "[0-9].*");
                } else {
                    tempBuffer.delete(0, "^".length());
                    if (pattern.endsWith(SPConstants.DOLLAR)) {
                        bDollar = true;
                        tempBuffer.delete(tempBuffer.length() - SPConstants.DOLLAR.length(),
                                tempBuffer.length());
                    }
                    try {
                        final URL urlPatt = new URL(tempBuffer.toString());
                        final int port = urlPatt.getPort();
                        final String strHost = urlPatt.getHost().toString();
                        if ((port == -1) && (strHost != null) && (strHost.length() != 0)) {
                            tempBuffer = new StringBuffer("^" + urlPatt.getProtocol() + SPConstants.URL_SEP
                                    + urlPatt.getHost() + ":[0-9].*" + urlPatt.getPath());
                        }
                        if (bDollar) {
                            tempBuffer.append(SPConstants.DOLLAR);
                        }
                    } catch (final Exception e) {
                        tempBuffer = new StringBuffer(pattern);
                    }
                }
            }

            try {
                new RE(tempBuffer);
            } catch (final Exception e) {
                invalidPatterns.add(pattern);
            }
            continue;
        }

        String patternDecoded = pattern;
        try {
            patternDecoded = URLDecoder.decode(pattern, "UTF-8");
        } catch (final Exception e) {
            // eatup exception. use the original value
            patternDecoded = pattern;
        }

        if (patternDecoded == null) {
            invalidPatterns.add(pattern);
        }

        boolean containProtocol = false;
        try {
            final RE re = new RE(SPConstants.URL_SEP);
            final REMatch reMatch = re.getMatch(patternDecoded);
            if (reMatch != null) {
                containProtocol = true; // protocol is present
            }

        } catch (final Exception e) {
            containProtocol = false;
        }

        if (containProtocol) {
            String urlPatt1stPart = null;
            String urlPatt2ndPart = null;
            boolean bPortStar = false;
            try {
                final URL urlPatt = new URL(patternDecoded);
                final int port = urlPatt.getPort();
                String strPort = "";
                if (port == -1) {
                    strPort = "[0-9].*";
                } else {
                    strPort = port + "";
                }
                urlPatt1stPart = "^" + urlPatt.getProtocol() + SPConstants.URL_SEP + urlPatt.getHost()
                        + SPConstants.COLON + strPort;
                if (!(urlPatt.getFile()).startsWith(SPConstants.SLASH)) { // The pattern must have "/" 
                                                                          // after the port
                    invalidPatterns.add(pattern);
                }
                urlPatt2ndPart = "^" + urlPatt.getFile();
            } catch (final Exception e) {
                bPortStar = true;
            }

            if (bPortStar) {
                final int indexOfStar = patternDecoded.indexOf("*");
                if (indexOfStar != -1) {
                    urlPatt1stPart = "^" + patternDecoded.substring(0, indexOfStar) + "[0-9].*";
                    if (!(patternDecoded.substring(indexOfStar + 1)).startsWith(SPConstants.SLASH)) {
                        invalidPatterns.add(pattern);
                    }
                    urlPatt2ndPart = "^" + patternDecoded.substring(indexOfStar + 1);
                }
            }

            try {
                new RE(urlPatt1stPart);
                new RE(urlPatt2ndPart);
            } catch (final Exception e) {
                invalidPatterns.add(pattern);
            }
        } else {
            String urlPatt1stPart = null;
            String urlPatt2ndPart = null;
            if (patternDecoded.indexOf(SPConstants.SLASH) != -1) {
                if (patternDecoded.indexOf(SPConstants.COLON) == -1) {
                    urlPatt1stPart = patternDecoded.substring(0, patternDecoded.indexOf(SPConstants.SLASH))
                            + ":[0-9].*";
                } else {
                    urlPatt1stPart = patternDecoded.substring(0, patternDecoded.indexOf(SPConstants.SLASH));
                }
                urlPatt2ndPart = patternDecoded.substring(patternDecoded.indexOf(SPConstants.SLASH));
            } else {
                invalidPatterns.add(pattern);
            }

            urlPatt1stPart = "^.*://.*" + urlPatt1stPart;
            urlPatt2ndPart = "^" + urlPatt2ndPart;
            try {
                new RE(urlPatt1stPart);
                new RE(urlPatt2ndPart);
            } catch (final Exception e) {
                invalidPatterns.add(pattern);
            }
        }
    }
    if (invalidPatterns.size() == 0) {
        return null;
    } else {
        return invalidPatterns;
    }
}

From source file:org.alfresco.solr.query.Solr4QueryParser.java

@SuppressWarnings("unchecked")
protected Query getFieldQueryImpl(String field, String queryText, AnalysisMode analysisMode,
        LuceneFunction luceneFunction) throws ParseException, IOException {
    // make sure the field exists or return a dummy query so we have no error ....ACE-3231
    SchemaField schemaField = schema.getFieldOrNull(field);
    boolean isNumeric = false;
    if (schemaField == null) {
        return new TermQuery(new Term("_dummy_", "_miss_"));
    } else {/*w w w .j  av a2 s.  co  m*/
        isNumeric = (schemaField.getType().getNumericType() != null);
    }

    // Use the analyzer to get all the tokens, and then build a TermQuery,
    // PhraseQuery, or noth

    // TODO: Untokenised columns with functions require special handling

    if (luceneFunction != LuceneFunction.FIELD) {
        throw new UnsupportedOperationException(
                "Field queries are not supported on lucene functions (UPPER, LOWER, etc)");
    }

    // if the incoming string already has a language identifier we strip it iff and addit back on again

    String localePrefix = "";

    String toTokenise = queryText;

    if (queryText.startsWith("{")) {
        int position = queryText.indexOf("}");
        if (position > 0) {
            String language = queryText.substring(0, position + 1);
            Locale locale = new Locale(queryText.substring(1, position));
            String token = queryText.substring(position + 1);
            boolean found = false;
            for (Locale current : Locale.getAvailableLocales()) {
                if (current.toString().equalsIgnoreCase(locale.toString())) {
                    found = true;
                    break;
                }
            }
            if (found) {
                localePrefix = language;
                toTokenise = token;
            } else {
                //toTokenise = token;
            }
        }
    }

    String testText = toTokenise;
    boolean requiresMLTokenDuplication = false;
    String localeString = null;
    if (isPropertyField(field) && (localePrefix.length() == 0)) {
        if ((queryText.length() > 0) && (queryText.charAt(0) == '\u0000')) {
            int position = queryText.indexOf("\u0000", 1);
            testText = queryText.substring(position + 1);
            requiresMLTokenDuplication = true;
            localeString = queryText.substring(1, position);

        }
    }

    // find the positions of any escaped * and ? and ignore them

    Set<Integer> wildcardPoistions = getWildcardPositions(testText);

    TokenStream source = null;
    ArrayList<org.apache.lucene.analysis.Token> list = new ArrayList<org.apache.lucene.analysis.Token>();
    boolean severalTokensAtSamePosition = false;
    org.apache.lucene.analysis.Token nextToken;
    int positionCount = 0;

    try {
        org.apache.lucene.analysis.Token reusableToken = new org.apache.lucene.analysis.Token();

        source = getAnalyzer().tokenStream(field, new StringReader(toTokenise));
        source.reset();
        while (source.incrementToken()) {
            CharTermAttribute cta = source.getAttribute(CharTermAttribute.class);
            OffsetAttribute offsetAtt = source.getAttribute(OffsetAttribute.class);
            TypeAttribute typeAtt = null;
            if (source.hasAttribute(TypeAttribute.class)) {
                typeAtt = source.getAttribute(TypeAttribute.class);
            }
            PositionIncrementAttribute posIncAtt = null;
            if (source.hasAttribute(PositionIncrementAttribute.class)) {
                posIncAtt = source.getAttribute(PositionIncrementAttribute.class);
            }
            nextToken = new Token(cta.buffer(), 0, cta.length(), offsetAtt.startOffset(),
                    offsetAtt.endOffset());
            if (typeAtt != null) {
                nextToken.setType(typeAtt.type());
            }
            if (posIncAtt != null) {
                nextToken.setPositionIncrement(posIncAtt.getPositionIncrement());
            }

            list.add(nextToken);
            if (nextToken.getPositionIncrement() != 0)
                positionCount += nextToken.getPositionIncrement();
            else
                severalTokensAtSamePosition = true;
        }
    } catch (SolrException e) {
        // MNT-15336
        // Text against a numeric field should fail silently rather then tell you it is not possible.
        if (isNumeric && e.getMessage() != null && e.getMessage().startsWith("Invalid Number:")) {
            // Generate a query that does not match any document - rather than nothing
            return createNoMatchQuery();
        } else {
            throw e;
        }
    } finally {
        try {
            if (source != null) {
                source.close();
            }
        } catch (IOException e) {
            // ignore
        }
    }

    // add any alpha numeric wildcards that have been missed
    // Fixes most stop word and wild card issues

    for (int index = 0; index < testText.length(); index++) {
        char current = testText.charAt(index);
        if (((current == '*') || (current == '?')) && wildcardPoistions.contains(index)) {
            StringBuilder pre = new StringBuilder(10);
            if (index == 0) {
                // "*" and "?" at the start

                boolean found = false;
                for (int j = 0; j < list.size(); j++) {
                    org.apache.lucene.analysis.Token test = list.get(j);
                    if ((test.startOffset() <= 0) && (0 < test.endOffset())) {
                        found = true;
                        break;
                    }
                }
                if (!found && (list.size() == 0)) {
                    // Add new token followed by * not given by the tokeniser
                    org.apache.lucene.analysis.Token newToken = new org.apache.lucene.analysis.Token("", 0, 0);
                    newToken.setType("ALPHANUM");
                    if (requiresMLTokenDuplication) {
                        Locale locale = I18NUtil.parseLocale(localeString);
                        MLTokenDuplicator duplicator = new MLTokenDuplicator(locale,
                                MLAnalysisMode.EXACT_LANGUAGE);
                        Iterator<org.apache.lucene.analysis.Token> it = duplicator.buildIterator(newToken);
                        if (it != null) {
                            int count = 0;
                            while (it.hasNext()) {
                                list.add(it.next());
                                count++;
                                if (count > 1) {
                                    severalTokensAtSamePosition = true;
                                }
                            }
                        }
                    }
                    // content
                    else {
                        list.add(newToken);
                    }
                }
            } else if (index > 0) {
                // Add * and ? back into any tokens from which it has been removed

                boolean tokenFound = false;
                for (int j = 0; j < list.size(); j++) {
                    org.apache.lucene.analysis.Token test = list.get(j);
                    if ((test.startOffset() <= index) && (index < test.endOffset())) {
                        if (requiresMLTokenDuplication) {
                            String termText = test.toString();
                            int position = termText.indexOf("}");
                            String language = termText.substring(0, position + 1);
                            String token = termText.substring(position + 1);
                            if (index >= test.startOffset() + token.length()) {
                                test.setEmpty();
                                test.append(language + token + current);
                            }
                        } else {
                            if (index >= test.startOffset() + test.length()) {
                                test.setEmpty();
                                test.append(test.toString() + current);
                            }
                        }
                        tokenFound = true;
                        break;
                    }
                }

                if (!tokenFound) {
                    for (int i = index - 1; i >= 0; i--) {
                        char c = testText.charAt(i);
                        if (Character.isLetterOrDigit(c)) {
                            boolean found = false;
                            for (int j = 0; j < list.size(); j++) {
                                org.apache.lucene.analysis.Token test = list.get(j);
                                if ((test.startOffset() <= i) && (i < test.endOffset())) {
                                    found = true;
                                    break;
                                }
                            }
                            if (found) {
                                break;
                            } else {
                                pre.insert(0, c);
                            }
                        } else {
                            break;
                        }
                    }
                    if (pre.length() > 0) {
                        // Add new token followed by * not given by the tokeniser
                        org.apache.lucene.analysis.Token newToken = new org.apache.lucene.analysis.Token(
                                pre.toString(), index - pre.length(), index);
                        newToken.setType("ALPHANUM");
                        if (requiresMLTokenDuplication) {
                            Locale locale = I18NUtil.parseLocale(localeString);
                            MLTokenDuplicator duplicator = new MLTokenDuplicator(locale,
                                    MLAnalysisMode.EXACT_LANGUAGE);
                            Iterator<org.apache.lucene.analysis.Token> it = duplicator.buildIterator(newToken);
                            if (it != null) {
                                int count = 0;
                                while (it.hasNext()) {
                                    list.add(it.next());
                                    count++;
                                    if (count > 1) {
                                        severalTokensAtSamePosition = true;
                                    }
                                }
                            }
                        }
                        // content
                        else {
                            list.add(newToken);
                        }
                    }
                }
            }

            StringBuilder post = new StringBuilder(10);
            if (index > 0) {
                for (int i = index + 1; i < testText.length(); i++) {
                    char c = testText.charAt(i);
                    if (Character.isLetterOrDigit(c)) {
                        boolean found = false;
                        for (int j = 0; j < list.size(); j++) {
                            org.apache.lucene.analysis.Token test = list.get(j);
                            if ((test.startOffset() <= i) && (i < test.endOffset())) {
                                found = true;
                                break;
                            }
                        }
                        if (found) {
                            break;
                        } else {
                            post.append(c);
                        }
                    } else {
                        break;
                    }
                }
                if (post.length() > 0) {
                    // Add new token followed by * not given by the tokeniser
                    org.apache.lucene.analysis.Token newToken = new org.apache.lucene.analysis.Token(
                            post.toString(), index + 1, index + 1 + post.length());
                    newToken.setType("ALPHANUM");
                    if (requiresMLTokenDuplication) {
                        Locale locale = I18NUtil.parseLocale(localeString);
                        MLTokenDuplicator duplicator = new MLTokenDuplicator(locale,
                                MLAnalysisMode.EXACT_LANGUAGE);
                        Iterator<org.apache.lucene.analysis.Token> it = duplicator.buildIterator(newToken);
                        if (it != null) {
                            int count = 0;
                            while (it.hasNext()) {
                                list.add(it.next());
                                count++;
                                if (count > 1) {
                                    severalTokensAtSamePosition = true;
                                }
                            }
                        }
                    }
                    // content
                    else {
                        list.add(newToken);
                    }
                }
            }

        }
    }

    // Put in real position increments as we treat them correctly

    int curentIncrement = -1;
    for (org.apache.lucene.analysis.Token c : list) {
        if (curentIncrement == -1) {
            curentIncrement = c.getPositionIncrement();
        } else if (c.getPositionIncrement() > 0) {
            curentIncrement = c.getPositionIncrement();
        } else {
            c.setPositionIncrement(curentIncrement);
        }
    }

    // Remove small bits already covered in larger fragments 
    list = getNonContained(list);

    Collections.sort(list, new Comparator<org.apache.lucene.analysis.Token>() {

        public int compare(Token o1, Token o2) {
            int dif = o1.startOffset() - o2.startOffset();
            return dif;

        }
    });

    // Combined * and ? based strings - should redo the tokeniser

    // Build tokens by position

    LinkedList<LinkedList<org.apache.lucene.analysis.Token>> tokensByPosition = new LinkedList<LinkedList<org.apache.lucene.analysis.Token>>();
    LinkedList<org.apache.lucene.analysis.Token> currentList = null;
    int lastStart = 0;
    for (org.apache.lucene.analysis.Token c : list) {
        if (c.startOffset() == lastStart) {
            if (currentList == null) {
                currentList = new LinkedList<org.apache.lucene.analysis.Token>();
                tokensByPosition.add(currentList);
            }
            currentList.add(c);
        } else {
            currentList = new LinkedList<org.apache.lucene.analysis.Token>();
            tokensByPosition.add(currentList);
            currentList.add(c);
        }
        lastStart = c.startOffset();
    }

    // Build all the token sequences and see which ones get strung together

    OrderedHashSet<LinkedList<org.apache.lucene.analysis.Token>> allTokenSequencesSet = new OrderedHashSet<LinkedList<org.apache.lucene.analysis.Token>>();
    for (LinkedList<org.apache.lucene.analysis.Token> tokensAtPosition : tokensByPosition) {
        OrderedHashSet<LinkedList<org.apache.lucene.analysis.Token>> positionalSynonymSequencesSet = new OrderedHashSet<LinkedList<org.apache.lucene.analysis.Token>>();

        OrderedHashSet<LinkedList<org.apache.lucene.analysis.Token>> newAllTokenSequencesSet = new OrderedHashSet<LinkedList<org.apache.lucene.analysis.Token>>();

        FOR_FIRST_TOKEN_AT_POSITION_ONLY: for (org.apache.lucene.analysis.Token t : tokensAtPosition) {
            org.apache.lucene.analysis.Token replace = new org.apache.lucene.analysis.Token(t, t.startOffset(),
                    t.endOffset());
            replace.setType(t.type());
            replace.setPositionIncrement(t.getPositionIncrement());

            boolean tokenFoundSequence = false;
            for (LinkedList<org.apache.lucene.analysis.Token> tokenSequence : allTokenSequencesSet) {
                LinkedList<org.apache.lucene.analysis.Token> newEntry = new LinkedList<org.apache.lucene.analysis.Token>();
                newEntry.addAll(tokenSequence);
                if ((newEntry.getLast().endOffset() == replace.endOffset())
                        && replace.type().equals(SynonymFilter.TYPE_SYNONYM)) {
                    if ((newEntry.getLast().startOffset() == replace.startOffset())
                            && newEntry.getLast().type().equals(SynonymFilter.TYPE_SYNONYM)) {
                        positionalSynonymSequencesSet.add(tokenSequence);
                        newEntry.add(replace);
                        tokenFoundSequence = true;
                    } else if (newEntry.getLast().type().equals(CommonGramsFilter.GRAM_TYPE)) {
                        if (newEntry.toString().endsWith(replace.toString())) {
                            // already in the gram
                            positionalSynonymSequencesSet.add(tokenSequence);
                            tokenFoundSequence = true;
                        } else {
                            // need to replace the synonym in the current gram
                            tokenFoundSequence = true;
                            StringBuffer old = new StringBuffer(newEntry.getLast().toString());
                            old.replace(replace.startOffset() - newEntry.getLast().startOffset(),
                                    replace.endOffset() - newEntry.getLast().startOffset(), replace.toString());
                            Token newToken = new org.apache.lucene.analysis.Token(old.toString(),
                                    newEntry.getLast().startOffset(), newEntry.getLast().endOffset());
                            newEntry.removeLast();
                            newEntry.add(newToken);
                        }
                    }
                } else if ((newEntry.getLast().startOffset() < replace.startOffset())
                        && (newEntry.getLast().endOffset() < replace.endOffset())) {
                    if (newEntry.getLast().type().equals(SynonymFilter.TYPE_SYNONYM)
                            && replace.type().equals(SynonymFilter.TYPE_SYNONYM)) {
                        positionalSynonymSequencesSet.add(tokenSequence);
                    }
                    newEntry.add(replace);
                    tokenFoundSequence = true;
                }
                newAllTokenSequencesSet.add(newEntry);
            }
            if (false == tokenFoundSequence) {
                for (LinkedList<org.apache.lucene.analysis.Token> tokenSequence : newAllTokenSequencesSet) {
                    LinkedList<org.apache.lucene.analysis.Token> newEntry = new LinkedList<org.apache.lucene.analysis.Token>();
                    newEntry.addAll(tokenSequence);
                    if ((newEntry.getLast().endOffset() == replace.endOffset())
                            && replace.type().equals(SynonymFilter.TYPE_SYNONYM)) {
                        if ((newEntry.getLast().startOffset() == replace.startOffset())
                                && newEntry.getLast().type().equals(SynonymFilter.TYPE_SYNONYM)) {
                            positionalSynonymSequencesSet.add(tokenSequence);
                            newEntry.add(replace);
                            tokenFoundSequence = true;
                        } else if (newEntry.getLast().type().equals(CommonGramsFilter.GRAM_TYPE)) {
                            if (newEntry.toString().endsWith(replace.toString())) {
                                // already in the gram
                                positionalSynonymSequencesSet.add(tokenSequence);
                                tokenFoundSequence = true;
                            } else {
                                // need to replace the synonym in the current gram
                                tokenFoundSequence = true;
                                StringBuffer old = new StringBuffer(newEntry.getLast().toString());
                                old.replace(replace.startOffset() - newEntry.getLast().startOffset(),
                                        replace.endOffset() - newEntry.getLast().startOffset(),
                                        replace.toString());
                                Token newToken = new org.apache.lucene.analysis.Token(old.toString(),
                                        newEntry.getLast().startOffset(), newEntry.getLast().endOffset());
                                newEntry.removeLast();
                                newEntry.add(newToken);
                                positionalSynonymSequencesSet.add(newEntry);
                            }
                        }
                    } else if ((newEntry.getLast().startOffset() < replace.startOffset())
                            && (newEntry.getLast().endOffset() < replace.endOffset())) {
                        if (newEntry.getLast().type().equals(SynonymFilter.TYPE_SYNONYM)
                                && replace.type().equals(SynonymFilter.TYPE_SYNONYM)) {
                            positionalSynonymSequencesSet.add(tokenSequence);
                            newEntry.add(replace);
                            tokenFoundSequence = true;
                        }
                    }
                }
            }
            if (false == tokenFoundSequence) {
                LinkedList<org.apache.lucene.analysis.Token> newEntry = new LinkedList<org.apache.lucene.analysis.Token>();
                newEntry.add(replace);
                newAllTokenSequencesSet.add(newEntry);
            }
            // Limit the max number of permutations we consider
            if (newAllTokenSequencesSet.size() > 64) {
                break FOR_FIRST_TOKEN_AT_POSITION_ONLY;
            }
        }
        allTokenSequencesSet = newAllTokenSequencesSet;
        allTokenSequencesSet.addAll(positionalSynonymSequencesSet);

    }

    LinkedList<LinkedList<org.apache.lucene.analysis.Token>> allTokenSequences = new LinkedList<LinkedList<org.apache.lucene.analysis.Token>>(
            allTokenSequencesSet);

    // build the unique

    LinkedList<LinkedList<org.apache.lucene.analysis.Token>> fixedTokenSequences = new LinkedList<LinkedList<org.apache.lucene.analysis.Token>>();
    for (LinkedList<org.apache.lucene.analysis.Token> tokenSequence : allTokenSequences) {
        LinkedList<org.apache.lucene.analysis.Token> fixedTokenSequence = new LinkedList<org.apache.lucene.analysis.Token>();
        fixedTokenSequences.add(fixedTokenSequence);
        org.apache.lucene.analysis.Token replace = null;
        for (org.apache.lucene.analysis.Token c : tokenSequence) {
            if (replace == null) {
                StringBuilder prefix = new StringBuilder();
                for (int i = c.startOffset() - 1; i >= 0; i--) {
                    char test = testText.charAt(i);
                    if (((test == '*') || (test == '?')) && wildcardPoistions.contains(i)) {
                        prefix.insert(0, test);
                    } else {
                        break;
                    }
                }
                String pre = prefix.toString();
                if (requiresMLTokenDuplication) {
                    String termText = c.toString();
                    int position = termText.indexOf("}");
                    String language = termText.substring(0, position + 1);
                    String token = termText.substring(position + 1);
                    replace = new org.apache.lucene.analysis.Token(language + pre + token,
                            c.startOffset() - pre.length(), c.endOffset());
                    replace.setType(c.type());
                    replace.setPositionIncrement(c.getPositionIncrement());
                } else {
                    String termText = c.toString();
                    replace = new org.apache.lucene.analysis.Token(pre + termText,
                            c.startOffset() - pre.length(), c.endOffset());
                    replace.setType(c.type());
                    replace.setPositionIncrement(c.getPositionIncrement());
                }
            } else {
                StringBuilder prefix = new StringBuilder();
                StringBuilder postfix = new StringBuilder();
                StringBuilder builder = prefix;
                for (int i = c.startOffset() - 1; i >= replace.endOffset(); i--) {
                    char test = testText.charAt(i);
                    if (((test == '*') || (test == '?')) && wildcardPoistions.contains(i)) {
                        builder.insert(0, test);
                    } else {
                        builder = postfix;
                        postfix.setLength(0);
                    }
                }
                String pre = prefix.toString();
                String post = postfix.toString();

                // Does it bridge?
                if ((pre.length() > 0) && (replace.endOffset() + pre.length()) == c.startOffset()) {
                    String termText = c.toString();
                    if (requiresMLTokenDuplication) {
                        int position = termText.indexOf("}");
                        @SuppressWarnings("unused")
                        String language = termText.substring(0, position + 1);
                        String token = termText.substring(position + 1);
                        int oldPositionIncrement = replace.getPositionIncrement();
                        String replaceTermText = replace.toString();
                        replace = new org.apache.lucene.analysis.Token(replaceTermText + pre + token,
                                replace.startOffset(), c.endOffset());
                        replace.setType(replace.type());
                        replace.setPositionIncrement(oldPositionIncrement);
                    } else {
                        int oldPositionIncrement = replace.getPositionIncrement();
                        String replaceTermText = replace.toString();
                        replace = new org.apache.lucene.analysis.Token(replaceTermText + pre + termText,
                                replace.startOffset(), c.endOffset());
                        replace.setType(replace.type());
                        replace.setPositionIncrement(oldPositionIncrement);
                    }
                } else {
                    String termText = c.toString();
                    if (requiresMLTokenDuplication) {
                        int position = termText.indexOf("}");
                        String language = termText.substring(0, position + 1);
                        String token = termText.substring(position + 1);
                        String replaceTermText = replace.toString();
                        org.apache.lucene.analysis.Token last = new org.apache.lucene.analysis.Token(
                                replaceTermText + post, replace.startOffset(),
                                replace.endOffset() + post.length());
                        last.setType(replace.type());
                        last.setPositionIncrement(replace.getPositionIncrement());
                        fixedTokenSequence.add(last);
                        replace = new org.apache.lucene.analysis.Token(language + pre + token,
                                c.startOffset() - pre.length(), c.endOffset());
                        replace.setType(c.type());
                        replace.setPositionIncrement(c.getPositionIncrement());
                    } else {
                        String replaceTermText = replace.toString();
                        org.apache.lucene.analysis.Token last = new org.apache.lucene.analysis.Token(
                                replaceTermText + post, replace.startOffset(),
                                replace.endOffset() + post.length());
                        last.setType(replace.type());
                        last.setPositionIncrement(replace.getPositionIncrement());
                        fixedTokenSequence.add(last);
                        replace = new org.apache.lucene.analysis.Token(pre + termText,
                                c.startOffset() - pre.length(), c.endOffset());
                        replace.setType(c.type());
                        replace.setPositionIncrement(c.getPositionIncrement());
                    }
                }
            }
        }
        // finish last
        if (replace != null) {
            StringBuilder postfix = new StringBuilder();
            if ((replace.endOffset() >= 0) && (replace.endOffset() < testText.length())) {
                for (int i = replace.endOffset(); i < testText.length(); i++) {
                    char test = testText.charAt(i);
                    if (((test == '*') || (test == '?')) && wildcardPoistions.contains(i)) {
                        postfix.append(test);
                    } else {
                        break;
                    }
                }
            }
            String post = postfix.toString();
            int oldPositionIncrement = replace.getPositionIncrement();
            String replaceTermText = replace.toString();
            replace = new org.apache.lucene.analysis.Token(replaceTermText + post, replace.startOffset(),
                    replace.endOffset() + post.length());
            replace.setType(replace.type());
            replace.setPositionIncrement(oldPositionIncrement);
            fixedTokenSequence.add(replace);
        }
    }

    // rebuild fixed list

    ArrayList<org.apache.lucene.analysis.Token> fixed = new ArrayList<org.apache.lucene.analysis.Token>();
    for (LinkedList<org.apache.lucene.analysis.Token> tokenSequence : fixedTokenSequences) {
        for (org.apache.lucene.analysis.Token token : tokenSequence) {
            fixed.add(token);
        }
    }

    // reorder by start position and increment

    Collections.sort(fixed, new Comparator<org.apache.lucene.analysis.Token>() {

        public int compare(Token o1, Token o2) {
            int dif = o1.startOffset() - o2.startOffset();
            if (dif != 0) {
                return dif;
            } else {
                return o1.getPositionIncrement() - o2.getPositionIncrement();
            }
        }
    });

    // make sure we remove any tokens we have duplicated

    @SuppressWarnings("rawtypes")
    OrderedHashSet unique = new OrderedHashSet();
    unique.addAll(fixed);
    fixed = new ArrayList<org.apache.lucene.analysis.Token>(unique);

    list = fixed;

    // add any missing locales back to the tokens

    if (localePrefix.length() > 0) {
        for (int j = 0; j < list.size(); j++) {
            org.apache.lucene.analysis.Token currentToken = list.get(j);
            String termText = currentToken.toString();
            currentToken.setEmpty();
            currentToken.append(localePrefix + termText);
        }
    }

    SchemaField sf = schema.getField(field);
    TokenizerChain tokenizerChain = (sf.getType().getQueryAnalyzer() instanceof TokenizerChain)
            ? ((TokenizerChain) sf.getType().getQueryAnalyzer())
            : null;
    boolean isShingled = false;
    if (tokenizerChain != null) {
        for (TokenFilterFactory factory : tokenizerChain.getTokenFilterFactories()) {
            if (factory instanceof ShingleFilterFactory) {
                isShingled = true;
                break;
            }
        }
    }
    AlfrescoAnalyzerWrapper analyzerWrapper = (sf.getType()
            .getQueryAnalyzer() instanceof AlfrescoAnalyzerWrapper)
                    ? ((AlfrescoAnalyzerWrapper) sf.getType().getQueryAnalyzer())
                    : null;
    if (analyzerWrapper != null) {
        // assume if there are no term positions it is shingled ....
        isShingled = true;
    }

    boolean forceConjuncion = rerankPhase == RerankPhase.QUERY_PHASE;

    if (list.size() == 0)
        return null;
    else if (list.size() == 1) {
        nextToken = list.get(0);
        String termText = nextToken.toString();
        if (!isNumeric && (termText.contains("*") || termText.contains("?"))) {
            return newWildcardQuery(new Term(field, termText));
        } else {
            return newTermQuery(new Term(field, termText));
        }
    } else {
        if (severalTokensAtSamePosition) {
            if (positionCount == 1) {
                // no phrase query:
                BooleanQuery q = newBooleanQuery(true);
                for (int i = 0; i < list.size(); i++) {
                    Query currentQuery;
                    nextToken = list.get(i);
                    String termText = nextToken.toString();
                    if (termText.contains("*") || termText.contains("?")) {
                        currentQuery = newWildcardQuery(new Term(field, termText));
                    } else {
                        currentQuery = newTermQuery(new Term(field, termText));
                    }
                    q.add(currentQuery, BooleanClause.Occur.SHOULD);
                }
                return q;
            } else if (forceConjuncion) {
                BooleanQuery or = new BooleanQuery();

                for (LinkedList<org.apache.lucene.analysis.Token> tokenSequence : fixedTokenSequences) {
                    BooleanQuery and = new BooleanQuery();
                    for (int i = 0; i < tokenSequence.size(); i++) {
                        nextToken = (org.apache.lucene.analysis.Token) tokenSequence.get(i);
                        String termText = nextToken.toString();

                        Term term = new Term(field, termText);
                        if ((termText != null) && (termText.contains("*") || termText.contains("?"))) {
                            org.apache.lucene.search.WildcardQuery wildQuery = new org.apache.lucene.search.WildcardQuery(
                                    term);
                            and.add(wildQuery, Occur.MUST);
                        } else {
                            TermQuery termQuery = new TermQuery(term);
                            and.add(termQuery, Occur.MUST);
                        }
                    }
                    if (and.clauses().size() > 0) {
                        or.add(and, Occur.SHOULD);
                    }
                }
                return or;
            }
            // shingle
            else if (sf.omitPositions() && isShingled) {

                ArrayList<org.apache.lucene.analysis.Token> nonContained = getNonContained(list);
                Query currentQuery;

                BooleanQuery weakPhrase = new BooleanQuery();
                for (org.apache.lucene.analysis.Token shingleToken : nonContained) {
                    String termText = shingleToken.toString();
                    Term term = new Term(field, termText);

                    if ((termText != null) && (termText.contains("*") || termText.contains("?"))) {
                        currentQuery = new org.apache.lucene.search.WildcardQuery(term);
                    } else {
                        currentQuery = new TermQuery(term);
                    }
                    weakPhrase.add(currentQuery, Occur.MUST);
                }

                return weakPhrase;

            }
            // Consider if we can use a multi-phrase query (e.g for synonym use rather then WordDelimiterFilterFactory)
            else if (canUseMultiPhraseQuery(fixedTokenSequences)) {
                // phrase query:
                MultiPhraseQuery mpq = newMultiPhraseQuery();
                mpq.setSlop(internalSlop);
                ArrayList<Term> multiTerms = new ArrayList<Term>();
                int position = 0;
                for (int i = 0; i < list.size(); i++) {
                    nextToken = list.get(i);
                    String termText = nextToken.toString();

                    Term term = new Term(field, termText);
                    if ((termText != null) && (termText.contains("*") || termText.contains("?"))) {
                        throw new IllegalStateException("Wildcards are not allowed in multi phrase anymore");
                    } else {
                        multiTerms.add(term);
                    }

                    if (nextToken.getPositionIncrement() > 0 && multiTerms.size() > 0) {
                        if (getEnablePositionIncrements()) {
                            mpq.add(multiTerms.toArray(new Term[0]), position);
                        } else {
                            mpq.add(multiTerms.toArray(new Term[0]));
                        }
                        checkTermCount(field, queryText, mpq);
                        multiTerms.clear();
                    }
                    position += nextToken.getPositionIncrement();

                }
                if (getEnablePositionIncrements()) {
                    if (multiTerms.size() > 0) {
                        mpq.add(multiTerms.toArray(new Term[0]), position);
                    }
                    //                        else
                    //                        {
                    //                            mpq.add(new Term[] { new Term(field, "\u0000") }, position);
                    //                        }
                } else {
                    if (multiTerms.size() > 0) {
                        mpq.add(multiTerms.toArray(new Term[0]));
                    }
                    //                        else
                    //                        {
                    //                            mpq.add(new Term[] { new Term(field, "\u0000") });
                    //                        }
                }
                checkTermCount(field, queryText, mpq);
                return mpq;

            }
            // Word delimiter factory and other odd things generate complex token patterns
            // Smart skip token  sequences with small tokens that generate toomany wildcards
            // Fall back to the larger pattern
            // e.g Site1* will not do (S ite 1*) or (Site 1*)  if 1* matches too much (S ite1*)  and (Site1*) will still be OK 
            // If we skip all (for just 1* in the input) this is still an issue.
            else {

                return generateSpanOrQuery(field, fixedTokenSequences);

            }
        } else {
            if (forceConjuncion) {
                BooleanQuery or = new BooleanQuery();

                for (LinkedList<org.apache.lucene.analysis.Token> tokenSequence : fixedTokenSequences) {
                    BooleanQuery and = new BooleanQuery();
                    for (int i = 0; i < tokenSequence.size(); i++) {
                        nextToken = (org.apache.lucene.analysis.Token) tokenSequence.get(i);
                        String termText = nextToken.toString();

                        Term term = new Term(field, termText);
                        if ((termText != null) && (termText.contains("*") || termText.contains("?"))) {
                            org.apache.lucene.search.WildcardQuery wildQuery = new org.apache.lucene.search.WildcardQuery(
                                    term);
                            and.add(wildQuery, Occur.MUST);
                        } else {
                            TermQuery termQuery = new TermQuery(term);
                            and.add(termQuery, Occur.MUST);
                        }
                    }
                    if (and.clauses().size() > 0) {
                        or.add(and, Occur.SHOULD);
                    }
                }
                return or;
            } else {
                SpanQuery spanQuery = null;
                SpanOrQuery atSamePosition = new SpanOrQuery();
                int gap = 0;
                for (int i = 0; i < list.size(); i++) {
                    nextToken = list.get(i);
                    String termText = nextToken.toString();
                    Term term = new Term(field, termText);
                    if (getEnablePositionIncrements()) {
                        SpanQuery nextSpanQuery;
                        if ((termText != null) && (termText.contains("*") || termText.contains("?"))) {
                            org.apache.lucene.search.WildcardQuery wildQuery = new org.apache.lucene.search.WildcardQuery(
                                    term);
                            SpanMultiTermQueryWrapper wrapper = new SpanMultiTermQueryWrapper<>(wildQuery);
                            wrapper.setRewriteMethod(
                                    new TopTermsSpanBooleanQueryRewrite(topTermSpanRewriteLimit));
                            nextSpanQuery = wrapper;
                        } else {
                            nextSpanQuery = new SpanTermQuery(term);
                        }
                        if (gap == 0) {
                            atSamePosition.addClause(nextSpanQuery);
                        } else {
                            if (atSamePosition.getClauses().length == 0) {
                                if (spanQuery == null) {
                                    spanQuery = nextSpanQuery;
                                } else {
                                    spanQuery = new SpanNearQuery(new SpanQuery[] { spanQuery, nextSpanQuery },
                                            (gap - 1) + internalSlop, internalSlop < 2);
                                }
                                atSamePosition = new SpanOrQuery();
                            } else if (atSamePosition.getClauses().length == 1) {
                                if (spanQuery == null) {
                                    spanQuery = atSamePosition.getClauses()[0];
                                } else {
                                    spanQuery = new SpanNearQuery(
                                            new SpanQuery[] { spanQuery, atSamePosition.getClauses()[0] },
                                            (gap - 1) + internalSlop, internalSlop < 2);
                                }
                                atSamePosition = new SpanOrQuery();
                                atSamePosition.addClause(nextSpanQuery);
                            } else {
                                if (spanQuery == null) {
                                    spanQuery = atSamePosition;
                                } else {
                                    spanQuery = new SpanNearQuery(new SpanQuery[] { spanQuery, atSamePosition },
                                            (gap - 1) + internalSlop, internalSlop < 2);
                                }
                                atSamePosition = new SpanOrQuery();
                                atSamePosition.addClause(nextSpanQuery);
                            }
                        }
                        gap = nextToken.getPositionIncrement();
                    } else {
                        SpanQuery nextSpanQuery;
                        if ((termText != null) && (termText.contains("*") || termText.contains("?"))) {
                            org.apache.lucene.search.WildcardQuery wildQuery = new org.apache.lucene.search.WildcardQuery(
                                    term);
                            SpanMultiTermQueryWrapper wrapper = new SpanMultiTermQueryWrapper<>(wildQuery);
                            wrapper.setRewriteMethod(
                                    new TopTermsSpanBooleanQueryRewrite(topTermSpanRewriteLimit));
                            nextSpanQuery = wrapper;
                        } else {
                            nextSpanQuery = new SpanTermQuery(term);
                        }
                        if (spanQuery == null) {
                            spanQuery = new SpanOrQuery();
                            ((SpanOrQuery) spanQuery).addClause(nextSpanQuery);
                        } else {
                            ((SpanOrQuery) spanQuery).addClause(nextSpanQuery);
                        }
                    }
                }
                if (atSamePosition.getClauses().length == 0) {
                    return spanQuery;
                } else if (atSamePosition.getClauses().length == 1) {
                    if (spanQuery == null) {
                        spanQuery = atSamePosition.getClauses()[0];
                    } else {
                        spanQuery = new SpanNearQuery(
                                new SpanQuery[] { spanQuery, atSamePosition.getClauses()[0] },
                                (gap - 1) + internalSlop, internalSlop < 2);
                    }
                    return spanQuery;
                } else {
                    if (spanQuery == null) {
                        spanQuery = atSamePosition;
                    } else {
                        spanQuery = new SpanNearQuery(new SpanQuery[] { spanQuery, atSamePosition },
                                (gap - 1) + internalSlop, internalSlop < 2);
                    }
                    return spanQuery;
                }
            }
        }
    }
}