Example usage for java.lang StringBuilder indexOf

List of usage examples for java.lang StringBuilder indexOf

Introduction

In this page you can find the example usage for java.lang StringBuilder indexOf.

Prototype

@Override
    public int indexOf(String str, int fromIndex) 

Source Link

Usage

From source file:com.dianping.resource.io.util.PropertyPlaceholderHelper.java

protected String parseStringValue(String strVal, PlaceholderResolver placeholderResolver,
        Set<String> visitedPlaceholders) {

    StringBuilder buf = new StringBuilder(strVal);

    int startIndex = strVal.indexOf(this.placeholderPrefix);
    while (startIndex != -1) {
        int endIndex = findPlaceholderEndIndex(buf, startIndex);
        if (endIndex != -1) {
            String placeholder = buf.substring(startIndex + this.placeholderPrefix.length(), endIndex);
            String originalPlaceholder = placeholder;
            if (!visitedPlaceholders.add(originalPlaceholder)) {
                throw new IllegalArgumentException(
                        "Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
            }//from   w w  w . j a v  a  2s  .c  o m
            // Recursive invocation, parsing placeholders contained in the placeholder key.
            placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
            // Now obtain the value for the fully resolved key...
            String propVal = placeholderResolver.resolvePlaceholder(placeholder);
            if (propVal == null && this.valueSeparator != null) {
                int separatorIndex = placeholder.indexOf(this.valueSeparator);
                if (separatorIndex != -1) {
                    String actualPlaceholder = placeholder.substring(0, separatorIndex);
                    String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
                    propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
                    if (propVal == null) {
                        propVal = defaultValue;
                    }
                }
            }
            if (propVal != null) {
                // Recursive invocation, parsing placeholders contained in the
                // previously resolved placeholder value.
                propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
                buf.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
                if (logger.isTraceEnabled()) {
                    logger.trace("Resolved placeholder '" + placeholder + "'");
                }
                startIndex = buf.indexOf(this.placeholderPrefix, startIndex + propVal.length());
            } else if (this.ignoreUnresolvablePlaceholders) {
                // Proceed with unprocessed value.
                startIndex = buf.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
            } else {
                throw new IllegalArgumentException("Could not resolve placeholder '" + placeholder + "'"
                        + " in string value \"" + strVal + "\"");
            }
            visitedPlaceholders.remove(originalPlaceholder);
        } else {
            startIndex = -1;
        }
    }

    return buf.toString();
}

From source file:org.springframework.util.PropertyPlaceholderHelper.java

protected String parseStringValue(String value, PlaceholderResolver placeholderResolver,
        Set<String> visitedPlaceholders) {

    StringBuilder result = new StringBuilder(value);

    int startIndex = value.indexOf(this.placeholderPrefix);
    while (startIndex != -1) {
        int endIndex = findPlaceholderEndIndex(result, startIndex);
        if (endIndex != -1) {
            String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
            String originalPlaceholder = placeholder;
            if (!visitedPlaceholders.add(originalPlaceholder)) {
                throw new IllegalArgumentException(
                        "Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
            }//from   www. java2s. c  o  m
            // Recursive invocation, parsing placeholders contained in the placeholder key.
            placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
            // Now obtain the value for the fully resolved key...
            String propVal = placeholderResolver.resolvePlaceholder(placeholder);
            if (propVal == null && this.valueSeparator != null) {
                int separatorIndex = placeholder.indexOf(this.valueSeparator);
                if (separatorIndex != -1) {
                    String actualPlaceholder = placeholder.substring(0, separatorIndex);
                    String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
                    propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
                    if (propVal == null) {
                        propVal = defaultValue;
                    }
                }
            }
            if (propVal != null) {
                // Recursive invocation, parsing placeholders contained in the
                // previously resolved placeholder value.
                propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
                result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
                if (logger.isTraceEnabled()) {
                    logger.trace("Resolved placeholder '" + placeholder + "'");
                }
                startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
            } else if (this.ignoreUnresolvablePlaceholders) {
                // Proceed with unprocessed value.
                startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
            } else {
                throw new IllegalArgumentException(
                        "Could not resolve placeholder '" + placeholder + "'" + " in value \"" + value + "\"");
            }
            visitedPlaceholders.remove(originalPlaceholder);
        } else {
            startIndex = -1;
        }
    }

    return result.toString();
}

From source file:costumetrade.common.util.PropertyPlaceholderHelper.java

protected String parseStringValue(String strVal, PlaceholderResolver placeholderResolver,
        Set<String> visitedPlaceholders) {

    StringBuilder result = new StringBuilder(strVal);

    int startIndex = strVal.indexOf(this.placeholderPrefix);
    while (startIndex != -1) {
        int endIndex = findPlaceholderEndIndex(result, startIndex);
        if (endIndex != -1) {
            String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
            String originalPlaceholder = placeholder;
            if (!visitedPlaceholders.add(originalPlaceholder)) {
                throw new IllegalArgumentException(
                        "Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
            }//from   w ww .ja v  a2s . c o  m
            // Recursive invocation, parsing placeholders contained in the placeholder key.
            placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
            // Now obtain the value for the fully resolved key...
            String propVal = placeholderResolver.resolvePlaceholder(placeholder);
            if (propVal == null && this.valueSeparator != null) {
                int separatorIndex = placeholder.indexOf(this.valueSeparator);
                if (separatorIndex != -1) {
                    String actualPlaceholder = placeholder.substring(0, separatorIndex);
                    String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
                    propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
                    if (propVal == null) {
                        propVal = defaultValue;
                    }
                }
            }
            if (propVal != null) {
                // Recursive invocation, parsing placeholders contained in the
                // previously resolved placeholder value.
                propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
                result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
                if (logger.isTraceEnabled()) {
                    logger.trace("Resolved placeholder '" + placeholder + "'");
                }
                startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
            } else if (this.ignoreUnresolvablePlaceholders) {
                // Proceed with unprocessed value.
                startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
            } else {
                throw new IllegalArgumentException("Could not resolve placeholder '" + placeholder + "'"
                        + " in string value \"" + strVal + "\"");
            }
            visitedPlaceholders.remove(originalPlaceholder);
        } else {
            startIndex = -1;
        }
    }

    return result.toString();
}

From source file:com.ocean.common.asserts.PropertyPlaceholderHelper.java

protected String parseStringValue(String value, PlaceholderResolver placeholderResolver,
        Set<String> visitedPlaceholders) {

    StringBuilder result = new StringBuilder(value);

    int startIndex = value.indexOf(this.placeholderPrefix);
    while (startIndex != -1) {
        int endIndex = findPlaceholderEndIndex(result, startIndex);
        if (endIndex != -1) {
            String placeholder = result.substring(startIndex + this.placeholderPrefix.length(), endIndex);
            String originalPlaceholder = placeholder;
            if (!visitedPlaceholders.add(originalPlaceholder)) {
                throw new IllegalArgumentException(
                        "Circular placeholder reference '" + originalPlaceholder + "' in property definitions");
            }/*from w  w  w .ja  va2 s.  co  m*/
            // Recursive invocation, parsing placeholders contained in the
            // placeholder key.
            placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders);
            // Now obtain the value for the fully resolved key...
            String propVal = placeholderResolver.resolvePlaceholder(placeholder);
            if (propVal == null && this.valueSeparator != null) {
                int separatorIndex = placeholder.indexOf(this.valueSeparator);
                if (separatorIndex != -1) {
                    String actualPlaceholder = placeholder.substring(0, separatorIndex);
                    String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
                    propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder);
                    if (propVal == null) {
                        propVal = defaultValue;
                    }
                }
            }
            if (propVal != null) {
                // Recursive invocation, parsing placeholders contained in
                // the
                // previously resolved placeholder value.
                propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders);
                result.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
                if (logger.isTraceEnabled()) {
                    logger.trace("Resolved placeholder '" + placeholder + "'");
                }
                startIndex = result.indexOf(this.placeholderPrefix, startIndex + propVal.length());
            } else if (this.ignoreUnresolvablePlaceholders) {
                // Proceed with unprocessed value.
                startIndex = result.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
            } else {
                throw new IllegalArgumentException(
                        "Could not resolve placeholder '" + placeholder + "'" + " in value \"" + value + "\"");
            }
            visitedPlaceholders.remove(originalPlaceholder);
        } else {
            startIndex = -1;
        }
    }

    return result.toString();
}

From source file:com.MainFiles.Functions.java

public String stringBuilder(String s) {
    StringBuilder sb = new StringBuilder(s);

    int i = 0;/*from w  w w  . ja  va 2  s  . c  o  m*/
    while ((i = sb.indexOf(" ", i + 30)) != -1) {
        sb.replace(i, i + 1, "#");
    }
    return sb.toString();
}

From source file:org.greencheek.utils.environment.propertyplaceholder.resolver.value.VariablePlaceholderValueResolver.java

private String parseStringValue(String strVal, Map<String, String> placeholderResolver,
        Set<String> visitedPlaceholders, boolean trimValues) {

    StringBuilder buf = new StringBuilder(strVal);

    int startIndex = strVal.indexOf(this.placeholderPrefix);
    while (startIndex != -1) {
        int endIndex = findPlaceholderEndIndex(buf, startIndex);
        if (endIndex != -1) {
            String placeholder = buf.substring(startIndex + this.placeholderPrefix.length(), endIndex);
            if (!visitedPlaceholders.add(placeholder)) {
                throw new IllegalArgumentException(
                        "Circular placeholder reference '" + placeholder + "' in property definitions");
            }//from   w ww.  j  a v  a 2  s .c o  m
            // Recursive invocation, parsing placeholders contained in the placeholder key.
            placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders, trimValues);

            // Now obtain the value for the fully resolved key...
            String propVal = placeholderResolver.get(placeholder);
            if (propVal == null && (this.resolvingEnvironmentVariables || this.resolvingSystemProperties)) {
                if (this.resolvingEnvironmentVariables) {
                    propVal = environmentProperties == null ? null
                            : environmentProperties.getProperty(placeholder);
                }
                if (this.resolvingSystemProperties) {
                    String temp = systemProperties == null ? null : systemProperties.getProperty(placeholder);
                    if (temp != null) {
                        propVal = temp;
                    }
                }
            }

            if (propVal == null && this.valueSeparator != null) {
                int separatorIndex = placeholder.indexOf(this.valueSeparator);
                if (separatorIndex != -1) {
                    String actualPlaceholder = placeholder.substring(0, separatorIndex);
                    String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length());
                    propVal = placeholderResolver.get(actualPlaceholder);
                    if (propVal == null) {
                        propVal = defaultValue;
                    }
                }
            }
            if (propVal != null) {
                // Recursive invocation, parsing placeholders contained in the
                // previously resolved placeholder value.
                propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders, trimValues);
                buf.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal);
                if (logger.isTraceEnabled()) {
                    logger.trace("Resolved placeholder '" + placeholder + "'");
                }
                startIndex = buf.indexOf(this.placeholderPrefix, startIndex + propVal.length());
            } else if (this.ignoreUnresolvablePlaceholders) {
                // Proceed with unprocessed value.
                startIndex = buf.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length());
            } else {
                throw new IllegalArgumentException("Could not resolve placeholder '" + placeholder + "'");
            }

            visitedPlaceholders.remove(placeholder);
        } else {
            startIndex = -1;
        }
    }

    if (trimValues) {
        return buf.toString().trim();
    } else {
        return buf.toString();
    }
}

From source file:meff.Function.java

private Map<String, String> compile() {
    class Compiler {
        private Map<String, StringBuilder> functions;
        private String currentFunctionName;
        private StringBuilder currentFunction;

        Compiler(String functionName) {
            functions = new HashMap<>();
            currentFunctionName = functionName;
            currentFunction = new StringBuilder();
            functions.put(currentFunctionName, currentFunction);
        }/*from   ww w  .  j  a  va2s  . com*/

        void compileLine(StringBuilder line) {
            // Manage score value features
            {
                Matcher m = Pattern.compile("(score_\\w+)(>=|>|<=|<|==)(-?\\d+)").matcher(line);
                while (m.find()) {
                    int offset = m.start();

                    String beginning = m.group(1);
                    String operator = m.group(2);
                    String end = m.group(3);

                    StringBuilder newScore = new StringBuilder();
                    switch (operator) {
                    case ">=":
                        newScore.append(beginning).append("_min=").append(end);
                        break;
                    case ">":
                        newScore.append(beginning).append("_min=").append(Integer.parseInt(end) + 1);
                        break;
                    case "<=":
                        newScore.append(beginning).append("=").append(end);
                        break;
                    case "<":
                        newScore.append(beginning).append("=").append(Integer.parseInt(end) - 1);
                        break;
                    case "==":
                        newScore.append(beginning).append("_min=").append(end).append(",").append(beginning)
                                .append("=").append(end);
                        break;
                    }

                    line.replace(offset, offset + m.group().length(), newScore.toString());

                    m.reset(); // Need to reset the matcher, so it updates the length of the text
                }
            }

            currentFunction.append(line).append("\n");
        }

        Map<String, String> getOutput() {
            Map<String, String> outputMap = new HashMap<>(functions.size());

            for (Map.Entry<String, StringBuilder> entry : functions.entrySet()) {
                outputMap.put(entry.getKey() + ".mcfunction", entry.getValue().toString());
            }

            return outputMap;
        }
    }

    Compiler compiler = new Compiler(getName());

    boolean comment = false;

    StringBuilder sb = new StringBuilder();

    for (String line : data.lines) {
        line = line.trim();
        if (line.startsWith("#") || line.startsWith("//")) {
            continue;
        }
        if (line.isEmpty()) {
            continue;
        }

        int si = 0; // start index
        int bci = -1; // block comment index
        int bcei = -1; // block comment end index
        int prevBci = -1;
        int prevBcei = -1;
        while ((!comment && (bci = line.indexOf("/*", si)) > -1)
                || (comment && (bcei = line.indexOf("*/", si)) > -1)) {
            if (comment) {
                if (line.charAt(bcei - 1) == '\\') {
                    si = bcei + 2;
                    bcei = prevBcei;
                    continue;
                }
                comment = false;
                si = bcei + 2;
            } else {
                if (line.charAt(bci - 1) == '\\') {
                    sb.append(line.substring(si, bci - 1)).append("/*");
                    si = bci + 2;
                    bci = prevBci;
                    continue;
                }
                sb.append(line.substring(si, bci));
                comment = true;
                si = bci + 2;
            }
            prevBci = bci;
            prevBcei = bcei;
        }

        if (comment) {
            continue;
        }

        if (line.endsWith("\\")) {
            line = line.substring(si, line.length() - 1);
            sb.append(line);
        } else {
            sb.append(line.substring(si));
            compiler.compileLine(sb);
            sb.delete(0, sb.length());
        }
    }

    return compiler.getOutput();
}

From source file:com.flexive.shared.scripting.groovy.GroovyScriptExporterTools.java

/**
 * Write the script code to create a group assignment
 *
 * @param ga                          the FxGroupAssignment to be scripted
 * @param isDerived                   the Assignment is derived
 * @param differences                 the List of differences (map keys f. the builder)
 * @param defaultsOnly                use only default settings provided by the GTB, no analysis of assignments will be performed
 * @param tabCount                    the number of tabs to be added to the code's left hand side
 * @param withoutDependencies         true = do not create assignment:xpath code
 * @param differingDerivedAssignments the List of assignment ids for derived assignments differing from their base assignments
 * @return returns the partial script as a String
 *///from   ww  w  .j  a va  2 s.  c o m
public static String updateGroupAssignment(FxGroupAssignment ga, boolean isDerived, List<String> differences,
        boolean defaultsOnly, int tabCount, boolean withoutDependencies,
        List<Long> differingDerivedAssignments) {
    StringBuilder script = new StringBuilder(200);
    final FxGroup group = ga.getGroup();

    // name = alias
    script.append(Indent.tabs(tabCount));
    boolean createGroup = false;
    if (!isDerived
            || (isDerived && differingDerivedAssignments != null
                    && !differingDerivedAssignments.contains(ga.getId()))
            || (isDerived && differences.size() > 0) || (isDerived && !withoutDependencies))
        createGroup = true;

    if (createGroup) {
        // script.append("\n");
        final String groupAlias = keyWordNameCheck(ga.getAlias().toUpperCase(), true);
        script.append(groupAlias).append("( "); // opening parenthesis + 1x \s
    }
    // ASSIGNMENT
    if (isDerived && !withoutDependencies || (isDerived && !withoutDependencies
            && differingDerivedAssignments != null && !differingDerivedAssignments.contains(ga.getId()))) {
        final String assignmentPath = CacheAdmin.getEnvironment().getAssignment(ga.getBaseAssignmentId())
                .getXPath();
        script.append("assignment: \"").append(assignmentPath).append("\",");
    }

    if (!defaultsOnly && differences.size() > 0) {
        tabCount++;
        script.append("\n");
        // label and hint
        if (differences.contains("hint") || differences.contains("label"))
            script.append(getLabelAndHintStructure(group, differences.contains("label"),
                    differences.contains("hint"), tabCount));

        Map<String, String> sopts = new LinkedHashMap<String, String>();

        if (differences.contains("defaultMultiplicity"))
            sopts.put("defaultMultiplicity", ga.getDefaultMultiplicity() + "");

        if (differences.contains("multiplicity")) {
            if (group.mayOverrideBaseMultiplicity())
                sopts.put("multiplicity", "new FxMultiplicity(" + ga.getMultiplicity().getMin() + ","
                        + ga.getMultiplicity().getMax() + ")");
        }

        if (differences.contains("groupMode"))
            sopts.put("groupMode", "GroupMode." + ga.getMode().name());

        if (differences.contains("enabled"))
            sopts.put("enabled", ga.isEnabled() + "");

        // FxStructureOptions via the GroovyOptionbuilder
        if (differences.contains("structureoptions"))
            script.append(getStructureOptions(ga, tabCount));

        // append options to script
        for (String option : sopts.keySet()) {
            script.append(simpleOption(option, sopts.get(option), tabCount));
        }

        script.trimToSize();
        if (script.indexOf(",\n", script.length() - 2) != -1)
            script.delete(script.length() - 2, script.length());

    }
    script.trimToSize();
    if (script.indexOf(",", script.length() - 1) != -1)
        script.delete(script.length() - 1, script.length());

    if (createGroup)
        script.append(") "); // closing parenthesis + 1x \s

    return script.toString();
}

From source file:com.jaeksoft.searchlib.parser.HtmlParser.java

@Override
protected void parseContent(StreamLimiter streamLimiter, LanguageEnum forcedLang)
        throws IOException, SearchLibException {

    titleBoost = getFloatProperty(ClassPropertyEnum.TITLE_BOOST);
    boostTagMap = new TreeMap<String, BoostTag>();
    boostTagMap.put("h1", new BoostTag(ClassPropertyEnum.H1_BOOST));
    boostTagMap.put("h2", new BoostTag(ClassPropertyEnum.H2_BOOST));
    boostTagMap.put("h3", new BoostTag(ClassPropertyEnum.H3_BOOST));
    boostTagMap.put("h4", new BoostTag(ClassPropertyEnum.H4_BOOST));
    boostTagMap.put("h5", new BoostTag(ClassPropertyEnum.H5_BOOST));
    boostTagMap.put("h6", new BoostTag(ClassPropertyEnum.H6_BOOST));
    ignoreMetaNoIndex = getBooleanProperty(ClassPropertyEnum.IGNORE_META_NOINDEX);
    ignoreMetaNoFollow = getBooleanProperty(ClassPropertyEnum.IGNORE_META_NOFOLLOW);
    ignoreLinkNoFollow = getBooleanProperty(ClassPropertyEnum.IGNORE_LINK_NOFOLLOW);
    ignoreUntitledDocuments = getBooleanProperty(ClassPropertyEnum.IGNORE_UNTITLED_DOCUMENTS);
    ignoreNonCanonical = getBooleanProperty(ClassPropertyEnum.IGNORE_NON_CANONICAL);

    String currentCharset = null;
    String headerCharset = null;//  www.  j a  v a  2  s  .c  o m
    String detectedCharset = null;

    IndexDocument sourceDocument = getSourceDocument();

    if (sourceDocument != null) {
        FieldValueItem fieldValueItem = sourceDocument
                .getFieldValue(UrlItemFieldEnum.INSTANCE.contentTypeCharset.getName(), 0);
        if (fieldValueItem != null)
            headerCharset = fieldValueItem.getValue();
        if (headerCharset == null) {
            fieldValueItem = sourceDocument.getFieldValue(UrlItemFieldEnum.INSTANCE.contentEncoding.getName(),
                    0);
            if (fieldValueItem != null)
                headerCharset = fieldValueItem.getValue();
        }
        currentCharset = headerCharset;
    }

    if (currentCharset == null) {
        detectedCharset = streamLimiter.getDetectedCharset();
        currentCharset = detectedCharset;
    }

    if (currentCharset == null) {
        currentCharset = getProperty(ClassPropertyEnum.DEFAULT_CHARSET).getValue();
    }

    String xPathExclusions = getProperty(ClassPropertyEnum.XPATH_EXCLUSION).getValue();
    Set<Object> xPathExclusionsSet = null;
    if (!StringUtils.isEmpty(xPathExclusions))
        xPathExclusionsSet = new HashSet<Object>();

    HtmlParserEnum htmlParserEnum = HtmlParserEnum.find(getProperty(ClassPropertyEnum.HTML_PARSER).getValue());

    HtmlDocumentProvider htmlProvider = getHtmlDocumentProvider(htmlParserEnum, currentCharset, streamLimiter,
            xPathExclusions, xPathExclusionsSet);
    if (htmlProvider == null)
        return;

    URL currentURL = htmlProvider.getBaseHref();
    IndexDocument srcDoc = getSourceDocument();
    try {
        if (currentURL == null)
            currentURL = LinkUtils.newEncodedURL(streamLimiter.getOriginURL());
        if (currentURL == null) {
            FieldValueItem fvi = srcDoc.getFieldValue(UrlItemFieldEnum.INSTANCE.url.getName(), 0);
            if (fvi != null)
                currentURL = LinkUtils.newEncodedURL(fvi.getValue());
        }
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }

    URL canonicalURL = htmlProvider.getCanonicalLink(currentURL);
    if (canonicalURL != null) {
        String canUrl = canonicalURL.toExternalForm();
        addDetectedLink(canUrl);
        if (ignoreNonCanonical) {
            String curUrl = currentURL.toExternalForm();
            if (!canUrl.equals(curUrl)) {
                isCanonical = false;
                return;
            }
        }
    }
    isCanonical = true;

    String title = htmlProvider.getTitle();
    if (ignoreUntitledDocuments)
        if (title == null || title.length() == 0)
            return;

    ParserResultItem result = getNewParserResultItem();

    addFieldTitle(result, title);

    result.addField(ParserFieldEnum.htmlProvider, htmlProvider.getName());

    // Check ContentType charset in meta http-equiv
    String metaCharset = htmlProvider.getMetaCharset();

    String selectedCharset = selectCharset(headerCharset, detectedCharset, metaCharset);

    if (selectedCharset != null) {
        if (!selectedCharset.equals(currentCharset)) {
            currentCharset = selectedCharset;
            htmlProvider = getHtmlDocumentProvider(htmlParserEnum, currentCharset, streamLimiter,
                    xPathExclusions, xPathExclusionsSet);
        }
    }

    StringWriter writer = new StringWriter();
    IOUtils.copy(streamLimiter.getNewInputStream(), writer, currentCharset);
    result.addField(ParserFieldEnum.htmlSource, writer.toString());
    writer.close();

    HtmlNodeAbstract<?> rootNode = htmlProvider.getRootNode();
    if (rootNode == null)
        return;

    for (HtmlNodeAbstract<?> metaNode : htmlProvider.getMetas()) {
        String metaName = metaNode.getAttributeText("name");
        if (metaName != null && metaName.startsWith(OPENSEARCHSERVER_FIELD)) {
            String field = metaName.substring(OPENSEARCHSERVER_FIELD_LENGTH);
            String[] fields = field.split("\\.");
            if (fields != null) {
                String content = metaNode.getAttributeText("content");
                result.addDirectFields(fields, content);
            }
        }
    }

    result.addField(ParserFieldEnum.charset, currentCharset);

    String metaRobots = null;

    String metaDcLanguage = null;

    String metaContentLanguage = null;

    for (HtmlNodeAbstract<?> node : htmlProvider.getMetas()) {
        String attr_name = node.getAttributeText("name");
        String attr_http_equiv = node.getAttributeText("http-equiv");
        if ("keywords".equalsIgnoreCase(attr_name))
            result.addField(ParserFieldEnum.meta_keywords, HtmlDocumentProvider.getMetaContent(node));
        else if ("description".equalsIgnoreCase(attr_name))
            result.addField(ParserFieldEnum.meta_description, HtmlDocumentProvider.getMetaContent(node));
        else if ("robots".equalsIgnoreCase(attr_name))
            metaRobots = HtmlDocumentProvider.getMetaContent(node);
        else if ("dc.language".equalsIgnoreCase(attr_name))
            metaDcLanguage = HtmlDocumentProvider.getMetaContent(node);
        else if ("content-language".equalsIgnoreCase(attr_http_equiv))
            metaContentLanguage = HtmlDocumentProvider.getMetaContent(node);
    }

    boolean metaRobotsFollow = true;
    boolean metaRobotsNoIndex = false;
    if (metaRobots != null) {
        metaRobots = metaRobots.toLowerCase();
        if (metaRobots.contains("noindex") && !ignoreMetaNoIndex) {
            metaRobotsNoIndex = true;
            result.addField(ParserFieldEnum.meta_robots, "noindex");
        }
        if (metaRobots.contains("nofollow") && !ignoreMetaNoFollow) {
            metaRobotsFollow = false;
            result.addField(ParserFieldEnum.meta_robots, "nofollow");
        }
    }

    UrlFilterItem[] urlFilterList = getUrlFilterList();

    boolean removeFragment = ClassPropertyEnum.KEEP_REMOVE_LIST[1]
            .equalsIgnoreCase(getProperty(ClassPropertyEnum.URL_FRAGMENT).getValue());

    List<HtmlNodeAbstract<?>> nodes = rootNode.getAllNodes("a", "frame", "img");
    if (srcDoc != null && nodes != null && metaRobotsFollow) {
        for (HtmlNodeAbstract<?> node : nodes) {
            String href = null;
            String rel = null;
            String nodeName = node.getNodeName();
            if ("a".equals(nodeName)) {
                href = node.getAttributeText("href");
                rel = node.getAttributeText("rel");
            } else if ("frame".equals(nodeName) || "img".equals(nodeName)) {
                href = node.getAttributeText("src");
            }
            boolean follow = true;
            if (rel != null)
                if (rel.contains("nofollow") && !ignoreLinkNoFollow)
                    follow = false;
            URL newUrl = null;
            if (href != null)
                if (!href.startsWith("javascript:"))
                    if (currentURL != null) {
                        href = StringEscapeUtils.unescapeXml(href);
                        newUrl = LinkUtils.getLink(currentURL, href, urlFilterList, removeFragment);
                    }
            if (newUrl != null) {
                ParserFieldEnum field = null;
                if (newUrl.getHost().equalsIgnoreCase(currentURL.getHost())) {
                    if (follow)
                        field = ParserFieldEnum.internal_link;
                    else
                        field = ParserFieldEnum.internal_link_nofollow;
                } else {
                    if (follow)
                        field = ParserFieldEnum.external_link;
                    else
                        field = ParserFieldEnum.external_link_nofollow;
                }
                String link = newUrl.toExternalForm();
                result.addField(field, link);
                if (follow)
                    addDetectedLink(link);
            }
        }
    }

    if (!metaRobotsNoIndex) {
        nodes = rootNode.getNodes("html", "body");
        if (nodes == null || nodes.size() == 0)
            nodes = rootNode.getNodes("html");
        if (nodes != null && nodes.size() > 0) {
            StringBuilder sb = new StringBuilder();
            getBodyTextContent(result, sb, nodes.get(0), true, null, 1024, xPathExclusionsSet);
            result.addField(ParserFieldEnum.body, sb);
        }
    }

    // Identification de la langue:
    Locale lang = null;
    String langMethod = null;
    String[] pathHtml = { "html" };
    nodes = rootNode.getNodes(pathHtml);
    if (nodes != null && nodes.size() > 0) {
        langMethod = "html lang attribute";
        String l = nodes.get(0).getAttributeText("lang");
        if (l != null)
            lang = Lang.findLocaleISO639(l);
    }
    if (lang == null && metaContentLanguage != null) {
        langMethod = "meta http-equiv content-language";
        lang = Lang.findLocaleISO639(metaContentLanguage);
    }
    if (lang == null && metaDcLanguage != null) {
        langMethod = "meta dc.language";
        lang = Lang.findLocaleISO639(metaDcLanguage);
    }

    if (lang != null) {
        result.addField(ParserFieldEnum.lang, lang.getLanguage());
        result.addField(ParserFieldEnum.lang_method, langMethod);
    } else if (!metaRobotsNoIndex)
        lang = result.langDetection(10000, ParserFieldEnum.body);

    if (getFieldMap().isMapped(ParserFieldEnum.generated_title)) {

        StringBuilder sb = new StringBuilder();
        try {
            sb.append(new URI(streamLimiter.getOriginURL()).getHost());
        } catch (URISyntaxException e) {
            Logging.error(e);
        }

        String generatedTitle = null;
        for (Map.Entry<String, BoostTag> entry : boostTagMap.entrySet()) {
            BoostTag boostTag = entry.getValue();
            if (boostTag.firstContent != null) {
                generatedTitle = boostTag.firstContent;
                break;
            }
        }

        if (generatedTitle == null) {
            final String FIELD_TITLE = "contents";

            MemoryIndex bodyMemoryIndex = new MemoryIndex();
            Analyzer bodyAnalyzer = new WhitespaceAnalyzer(Version.LUCENE_36);
            String bodyText = result.getMergedBodyText(100000, " ", ParserFieldEnum.body);
            bodyMemoryIndex.addField(FIELD_TITLE, bodyText, bodyAnalyzer);

            IndexSearcher indexSearcher = bodyMemoryIndex.createSearcher();
            IndexReader indexReader = indexSearcher.getIndexReader();
            MoreLikeThis mlt = new MoreLikeThis(indexReader);
            mlt.setAnalyzer(bodyAnalyzer);
            mlt.setFieldNames(new String[] { FIELD_TITLE });
            mlt.setMinWordLen(3);
            mlt.setMinTermFreq(1);
            mlt.setMinDocFreq(1);

            String[] words = mlt.retrieveInterestingTerms(0);
            if (words != null && words.length > 0)
                generatedTitle = words[0];
        }

        if (generatedTitle != null) {
            if (sb.length() > 0)
                sb.append(" - ");
            sb.append(generatedTitle);
        }

        if (sb.length() > 67) {
            int pos = sb.indexOf(" ", 60);
            if (pos == -1)
                pos = 67;
            sb.setLength(pos);
            sb.append("...");
        }
        result.addField(ParserFieldEnum.generated_title, sb.toString());
    }

}

From source file:com.flexive.shared.scripting.groovy.GroovyScriptExporterTools.java

/**
 * This method generates the script code for a type
 *
 * @param type         the FxType which should be exported
 * @param defaultsOnly use defaults only, do not analyse / script options
 * @param addWorkflow  add the type's current workflow to the code
 * @return String returns the script code for a type
 *///from ww w.  j  a  v  a 2 s. c  o  m
public static String createType(FxType type, boolean defaultsOnly, boolean addWorkflow) {
    StringBuilder script = new StringBuilder(500);
    final int tabCount = 1;

    script.append("builder = new GroovyTypeBuilder().");
    final String typeName = keyWordNameCheck(type.getName(), true);
    script.append(typeName.toLowerCase()).append("( "); // opening parenthesis + 1x \s

    if (!defaultsOnly) {
        script.append("\n");
        // LABEL
        final long[] langs = type.getLabel().getTranslatedLanguages();
        final long defLang = type.getLabel().getDefaultLanguage();
        script.append("\tlabel: new FxString(true, ").append(defLang).append(", \"").append(type.getLabel())
                .append("\")");

        if (langs.length > 1) { // we have more than one language assignment
            for (long id : langs) {
                if (id != defLang) {
                    script.append(".setTranslation(").append(id).append(", \"")
                            .append(type.getLabel().getTranslation(id)).append("\")");
                }
            }
        }
        script.append(",\n");

        // sopts - a map for "simple" GroovyTypeBuilder options
        Map<String, String> sopts = new LinkedHashMap<String, String>();
        // type acl
        String acl = type.getACL().getName();
        // only set if different from the default structure ACL
        if (!CacheAdmin.getEnvironment().getACL(acl)
                .equals(CacheAdmin.getEnvironment().getACL(ACLCategory.STRUCTURE.getDefaultId()))) {
            sopts.put("acl", "\"" + acl + "\"");
        }

        // type defaultInstanceACL
        if (type.hasDefaultInstanceACL()) {
            String defInstACL = type.getDefaultInstanceACL().getName();
            sopts.put("defaultInstanceACL", "\"" + defInstACL + "\"");
        }
        sopts.put("languageMode",
                type.getLanguage() == LanguageMode.Multiple ? "LanguageMode.Multiple" : "LanguageMode.Single");
        sopts.put("trackHistory", type.isTrackHistory() + "");
        if (type.isTrackHistory())
            sopts.put("historyAge", type.getHistoryAge() + "L");
        sopts.put("typeMode", "TypeMode." + type.getMode().name() + "");
        // sopts.put("workFlow", ",\n") /* Left out for now, also needs to be added in GroovyTypeBuilder */
        sopts.put("maxVersions", type.getMaxVersions() + "L");
        sopts.put("storageMode", "TypeStorageMode." + type.getStorageMode().name() + ""); // not supported in FxTypeEdit, needs to be added to groovy builder
        sopts.put("useInstancePermissions", type.isUseInstancePermissions() + "");
        sopts.put("usePropertyPermissions", type.isUsePropertyPermissions() + "");
        sopts.put("useStepPermissions", type.isUseStepPermissions() + "");
        sopts.put("useTypePermissions", type.isUseTypePermissions() + "");
        sopts.put("usePermissions", type.isUsePermissions() + "");
        if (addWorkflow) {
            sopts.put("workflow", "\"" + type.getWorkflow().getName() + "\"");
        }
        if (type.isDerived()) { // take out of !defaultsOnly option?
            // if clause necessary since rev. #2162 (all types derived from ROOT)
            if (!FxType.ROOT.equals(type.getParent().getName()))
                sopts.put("parentTypeName", "\"" + type.getParent().getName() + "\"");
        }

        // FxStructureOptions via the GroovyOptionbuilder
        script.append(getStructureOptions(type, tabCount));

        // append options to script
        for (String option : sopts.keySet()) {
            script.append(simpleOption(option, sopts.get(option), tabCount));
        }

        script.trimToSize();
        if (script.indexOf(",\n", script.length() - 2) != -1)
            script.delete(script.length() - 2, script.length());

    }

    script.append(")\n\n");
    script.trimToSize();
    return script.toString();
}