List of usage examples for java.lang StringBuilder indexOf
@Override public int indexOf(String str, int fromIndex)
From source file:com.cohort.util.String2.java
/** * This counts all occurrences of <tt>findS</tt> in sb. * if (sb == null || findS == null || findS.length() == 0) return 0; * //from w ww. ja v a 2 s . c o m * @param sb the source StringBuilder * @param findS the string to be searched for */ public static int countAll(StringBuilder sb, String findS) { if (sb == null || findS == null || findS.length() == 0) return 0; int n = 0; int sLength = findS.length(); int po = sb.indexOf(findS, 0); while (po >= 0) { n++; po = sb.indexOf(findS, po + sLength); } return n; }
From source file:com.quinsoft.zeidon.vml.VmlOperation.java
protected static final int zstrstr(StringBuilder sbString, int nOffset, String subString) { return sbString.indexOf(subString, nOffset); }
From source file:com.flexive.shared.scripting.groovy.GroovyScriptExporterTools.java
/** * Write the script code to create a property from a given FxPropertyAssignment * * @param pa the FxPropertyAssignment to be scripted * @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 StringBuilder instance *//*from w w w .j av a2 s . co m*/ public static String createProperty(FxPropertyAssignment pa, boolean defaultsOnly, int tabCount, boolean withoutDependencies, List<Long> differingDerivedAssignments) { final FxProperty prop = pa.getProperty(); StringBuilder script = new StringBuilder(1000); // NAME script.append(Indent.tabs(tabCount)); tabCount++; final String propName = keyWordNameCheck(prop.getName().toLowerCase(), true); script.append(propName).append("( "); // opening parenthesis + 1x \s if (!defaultsOnly) { script.append("\n"); // label and hint script.append(getLabelAndHintStructure(prop, true, true, tabCount)); final String dataType = prop.getDataType() + ""; // sopts - a map for "simple" GroovyTypeBuilder options Map<String, String> sopts = new LinkedHashMap<String, String>(); // ALIAS --> set if different from name if (!prop.getName().equals(pa.getAlias())) { sopts.put("alias", "\"" + pa.getAlias() + "\""); } // def multiplicity for the assignment sopts.put("defaultMultiplicity", pa.getDefaultMultiplicity() + ""); sopts.put("multilang", prop.isMultiLang() + ""); sopts.put("dataType", "FxDataType." + dataType + ""); sopts.put("acl", "CacheAdmin.getEnvironment().getACL(ACLCategory." + prop.getACL().getCategory() + ".getDefaultId())"); sopts.put("multiplicity", "new FxMultiplicity(" + prop.getMultiplicity().getMin() + "," + prop.getMultiplicity().getMax() + ")"); sopts.put("overrideACL", prop.mayOverrideACL() + ""); sopts.put("overrideMultiplicity", prop.mayOverrideBaseMultiplicity() + ""); if (prop.hasOption(FxStructureOption.OPTION_SHOW_OVERVIEW)) sopts.put("overrideInOverview", prop.mayOverrideInOverview() + ""); if (prop.hasOption(FxStructureOption.OPTION_MAXLENGTH)) sopts.put("overrideMaxLength", prop.mayOverrideMaxLength() + ""); if (prop.hasOption(FxStructureOption.OPTION_MULTILINE)) sopts.put("overrideMultiline", prop.mayOverrideMultiLine() + ""); if (prop.hasOption(FxStructureOption.OPTION_SEARCHABLE)) sopts.put("overrideSearchable", prop.mayOverrideSearchable() + ""); if (prop.hasOption(FxStructureOption.OPTION_HTML_EDITOR)) sopts.put("overrideUseHtmlEditor", prop.mayOverrideUseHTMLEditor() + ""); if (prop.hasOption(FxStructureOption.OPTION_MULTILANG)) sopts.put("overrideMultilang", prop.mayOverrideMultiLang() + ""); if (prop.getMaxLength() != 0) {// means that maxLength is not set sopts.put("maxLength", prop.getMaxLength() + ""); } sopts.put("searchable", prop.isSearchable() + ""); sopts.put("fullTextIndexed", prop.isFulltextIndexed() + ""); sopts.put("multiline", prop.isMultiLine() + ""); sopts.put("inOverview", prop.isInOverview() + ""); sopts.put("useHtmlEditor", prop.isUseHTMLEditor() + ""); sopts.put("uniqueMode", "UniqueMode." + prop.getUniqueMode()); sopts.put("enabled", pa.isEnabled() + ""); // REFERENCE if ("Reference".equals(dataType)) { final String refType = "CacheAdmin.getEnvironment().getType(\"" + prop.getReferencedType().getName() + "\")"; sopts.put("referencedType", refType + ""); } if (prop.getReferencedList() != null) { final String refList = "CacheAdmin.getEnvironment().getSelectList(\"" + prop.getReferencedList().getName() + "\")"; sopts.put("referencedList", refList + ""); } // FxStructureOptions via the GroovyOptionbuilder script.append(getStructureOptions(prop, tabCount)); // DEFAULT VALUES if (prop.isDefaultValueSet()) { final FxValue val = prop.getDefaultValue(); String defaultValue = val.toString(); StringBuilder out = new StringBuilder(100); final String multiLang = prop.isMultiLang() + ""; if (DATATYPES.contains(dataType)) { // SELECT LIST DATATYPES if ("SelectOne".equals(dataType) || "SelectMany".equals(dataType)) { final String refListName = "CacheAdmin.getEnvironment().getSelectList(\"" + prop.getReferencedList().getName(); sopts.put("referencedList", refListName + "\"),\n"); final FxSelectList list = prop.getReferencedList(); if ("SelectOne".equals(dataType)) { for (FxSelectListItem item : list.getItems()) { if (defaultValue.equals(item.getLabel().toString())) { defaultValue = item.getName(); // reassign } } out.append("new FxSelectOne(").append(multiLang) .append(", CacheAdmin.getEnvironment().getSelectListItem(").append(refListName) .append("\"), \"").append(defaultValue).append("\"))"); } else if ("SelectMany".equals(dataType)) { String[] defaults = FxSharedUtils.splitLiterals(defaultValue); for (int i = 0; i < defaults.length; i++) { for (FxSelectListItem item : list.getItems()) { if (defaults[i].equals(item.getLabel().toString())) { defaults[i] = item.getName(); // reassign } } } out.append("new FxSelectMany(").append(multiLang).append(", new SelectMany(") .append(refListName).append("\"))"); // traverse renamed defaults and append them to the script for (String d : defaults) { out.append(".selectItem(CacheAdmin.getEnvironment().getSelectListItem(") .append(refListName).append("\"), \"").append(d).append("\"))"); } out.append(")"); } } else if ("Date".equals(dataType) || "DateTime".equals(dataType) || "DateRange".equals(dataType) || "DateTimeRange".equals(dataType)) { final String df = "\"MMM dd, yyyy\""; final String dtf = "\"MMM dd, yyyy h:mm:ss a\""; if ("Date".equals(dataType)) { out.append("new FxDate(").append(multiLang).append(", new SimpleDateFormat(").append(df) .append(").parse(\"").append(defaultValue).append("\"))"); } else if ("DateTime".equals(dataType)) { out.append("new FxDateTime(").append(multiLang).append(", new SimpleDateFormat(") .append(dtf).append(").parse(\"").append(defaultValue).append("\"))"); } else if ("DateRange".equals(dataType)) { final String lower = stripToEmpty(defaultValue.substring(0, defaultValue.indexOf("-"))); final String upper = stripToEmpty( defaultValue.substring(defaultValue.indexOf("-") + 1)); out.append("new FxDateRange(").append(multiLang) .append(", new DateRange(new SimpleDateFormat(").append(df).append(").parse(\"") .append(lower).append("\"), new SimpleDateFormat(").append(df) .append(").parse(\"").append(upper).append("\")))"); } else if ("DateTimeRange".equals(dataType)) { final String lower = stripToEmpty(defaultValue.substring(0, defaultValue.indexOf("-"))); final String upper = stripToEmpty( defaultValue.substring(defaultValue.indexOf("-") + 1)); out.append("new FxDateTimeRange(").append(multiLang) .append(", new DateRange(new SimpleDateFormat(").append(dtf) .append(").parse(\"").append(lower).append("\"), new SimpleDateFormat(") .append(dtf).append(").parse(\"").append(upper).append("\")))"); } } else if ("Reference".equals(dataType)) { final FxPK pk = FxPK.fromString(defaultValue); out.append("new FxReference(").append(multiLang).append(", new ReferencedContent(") .append(pk.getId()).append(", ").append(pk.getVersion()).append("))"); } else if ("InlineReference".equals(dataType)) { // ignore for now, doesn't work properly as of yet } else if ("Binary".equals(dataType)) { // uses a new BinaryDescriptor( .. ) } } // "SIMPLE" dataTYPES if (DATATYPESSIMPLE.keySet().contains(dataType)) { for (String d : DATATYPESSIMPLE.keySet()) { if (d.equals(dataType)) { out.append(DATATYPESSIMPLE.get(d)).append("(").append(multiLang).append(", "); if (d.equals("Float") || d.equals("Double") || d.equals("LargeNumber") || d.equals("Boolean")) { out.append(defaultValue); } else { out.append("\"").append(defaultValue).append("\""); } out.append(")"); } } } out.trimToSize(); // add the computed value to the "simpleOtions" sopts.put("defaultValue", out.toString() + ","); } // 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()); if (script.indexOf(",", script.length() - 1) != -1) script.delete(script.length() - 1, script.length()); } script.append(")\n"); // closing parenthesis // if the difference analyser yields any data, change the assignment in the next line (only launch if defaultsOnly = false) if (!defaultsOnly) { final List<String> differences = AssignmentDifferenceAnalyser.analyse(pa, false); if (differences.size() > 0) { script.append(updatePropertyAssignment(pa, false, differences, defaultsOnly, --tabCount, withoutDependencies, differingDerivedAssignments)); } } script.trimToSize(); return script.toString(); }
From source file:com.flexive.shared.scripting.groovy.GroovyScriptExporterTools.java
/** * Write the script code to update a property assignment, or to create a derived assignment * <p/>//from www . j av a2 s . co m * "acl", "defaultValue", "hint", "label", "multilang", "multiline", "multiplicity" * * @param pa the FxPropertyAssignment to be updated * @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 StringBuilder instance */ public static String updatePropertyAssignment(FxPropertyAssignment pa, boolean isDerived, List<String> differences, boolean defaultsOnly, int tabCount, boolean withoutDependencies, List<Long> differingDerivedAssignments) { StringBuilder script = new StringBuilder(500); final FxProperty prop = pa.getProperty(); final String dataType = pa.getProperty().getDataType() + ""; // use the alias as the reference name script.append(Indent.tabs(tabCount)); boolean createProp = false; if (!isDerived || (isDerived && differingDerivedAssignments != null && !differingDerivedAssignments.contains(pa.getId())) || (isDerived && differences.size() > 0) || (isDerived && !withoutDependencies)) createProp = true; if (createProp) { final String propAlias = keyWordNameCheck(pa.getAlias().toLowerCase(), true); script.append(propAlias).append("( "); // opening parenthesis + 1x \s } // ASSIGNMENT if (isDerived && !withoutDependencies || (isDerived && !withoutDependencies && differingDerivedAssignments != null && !differingDerivedAssignments.contains(pa.getId()))) { final String assignmentPath = CacheAdmin.getEnvironment().getAssignment(pa.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(getLabelAndHintAssignment(pa, differences.contains("label"), differences.contains("hint"), tabCount)); // sopts - a map for "simple" GroovyTypeBuilder options Map<String, String> sopts = new LinkedHashMap<String, String>(); final String multiLang = pa.isMultiLang() + ""; if (differences.contains("multilang")) { if (prop.mayOverrideMultiLang()) { sopts.put("multilang", multiLang + ""); } } if (differences.contains("acl")) { if (prop.mayOverrideACL()) sopts.put("acl", "CacheAdmin.getEnvironment().getACL(ACLCategory." + pa.getACL().getCategory() + ".getDefaultId())"); } if (differences.contains("multiplicity")) { if (prop.mayOverrideBaseMultiplicity()) sopts.put("multiplicity", "new FxMultiplicity(" + pa.getMultiplicity().getMin() + "," + pa.getMultiplicity().getMax() + ")"); } if (differences.contains("maxLength")) { if (prop.mayOverrideMaxLength()) sopts.put("maxLength", pa.getMaxLength() + ""); } if (differences.contains("inOverview")) { if (prop.mayOverrideInOverview()) sopts.put("inOverview", pa.isInOverview() + ""); } if (differences.contains("useHtmlEditor")) { if (prop.mayOverrideUseHTMLEditor()) sopts.put("useHtmlEditor", pa.isUseHTMLEditor() + ""); } if (differences.contains("multilang")) { if (pa.isMultiLang()) { sopts.put("", pa.getDefaultLanguage() + ""); } } if (differences.contains("multiline")) { if (prop.mayOverrideMultiLine()) sopts.put("multiline", pa.isMultiLine() + ""); } // FxStructureOptions via the GroovyOptionbuilder if (differences.contains("structureoptions")) script.append(getStructureOptions(pa, tabCount)); if (differences.contains("searchable")) { if (prop.mayOverrideSearchable()) sopts.put("searchable", pa.isSearchable() + ""); } // options valid for derived assignments only ********************** if (differences.contains("defaultMultiplicity")) sopts.put("defaultMultiplicity", pa.getDefaultMultiplicity() + ""); if (differences.contains("alias")) sopts.put("alias", "\"" + pa.getAlias() + "\""); if (differences.contains("enabled")) sopts.put("enabled", pa.isEnabled() + ""); if (differences.contains("defaultLanguage")) sopts.put("defaultLanguage", pa.getDefaultLanguage() + "L"); // ***************************************************************** // DEFAULT VALUES if (differences.contains("defaultValue")) { if (pa.getDefaultValue() != null) { final FxValue val = pa.getDefaultValue(); String defaultValue = val.toString(); StringBuilder out = new StringBuilder(100); if (DATATYPES.contains(dataType)) { // SELECT LIST DATATYPES if ("SelectOne".equals(dataType) || "SelectMany".equals(dataType)) { final FxSelectList list = pa.getProperty().getReferencedList(); final String refListName = "CacheAdmin.getEnvironment().getSelectList(\"" + list.getName(); sopts.put("referencedList", refListName + "\"),\n"); if ("SelectOne".equals(dataType)) { for (FxSelectListItem item : list.getItems()) { if (defaultValue.equals(item.getLabel().toString())) { defaultValue = item.getName(); // reassign } } out.append("new FxSelectOne(").append(multiLang) .append(", CacheAdmin.getEnvironment().getSelectListItem(") .append(refListName).append("\"), \"").append(defaultValue).append("\"))"); } else if ("SelectMany".equals(dataType)) { String[] defaults = FxSharedUtils.splitLiterals(defaultValue); for (int i = 0; i < defaults.length; i++) { for (FxSelectListItem item : list.getItems()) { if (defaults[i].equals(item.getLabel().toString())) { defaults[i] = item.getName(); // reassign } } } out.append("new FxSelectMany(").append(multiLang).append(", new SelectMany(") .append(refListName).append("\"))"); // traverse renamed defaults and append them to the script for (String d : defaults) { out.append(".selectItem(CacheAdmin.getEnvironment().getSelectListItem(") .append(refListName).append("\"), \"").append(d).append("\"))"); } out.append(")"); } } else if ("Date".equals(dataType) || "DateTime".equals(dataType) || "DateRange".equals(dataType) || "DateTimeRange".equals(dataType)) { final String df = "\"MMM dd, yyyy\""; final String dtf = "\"MMM dd, yyyy h:mm:ss a\""; if ("Date".equals(dataType)) { out.append("new FxDate(").append(multiLang).append(", new SimpleDateFormat(") .append(df).append(").parse(\"").append(defaultValue).append("\"))"); } else if ("DateTime".equals(dataType)) { out.append("new FxDateTime(").append(multiLang).append(", new SimpleDateFormat(") .append(dtf).append(").parse(\"").append(defaultValue).append("\"))"); } else if ("DateRange".equals(dataType)) { final String lower = stripToEmpty( defaultValue.substring(0, defaultValue.indexOf("-"))); final String upper = stripToEmpty( defaultValue.substring(defaultValue.indexOf("-") + 1)); out.append("new FxDateRange(").append(multiLang) .append(", new DateRange(new SimpleDateFormat(").append(df) .append(").parse(\"").append(lower).append("\"), new SimpleDateFormat(") .append(df).append(").parse(\"").append(upper).append("\")))"); } else if ("DateTimeRange".equals(dataType)) { final String lower = stripToEmpty( defaultValue.substring(0, defaultValue.indexOf("-"))); final String upper = stripToEmpty( defaultValue.substring(defaultValue.indexOf("-") + 1)); out.append("new FxDateTimeRange(").append(multiLang) .append(", new DateRange(new SimpleDateFormat(").append(dtf) .append(").parse(\"").append(lower).append("\"), new SimpleDateFormat(") .append(dtf).append(").parse(\"").append(upper).append("\")))"); } } else if ("Reference".equals(dataType)) { final FxPK pk = FxPK.fromString(defaultValue); out.append("new FxReference(").append(multiLang).append(", new ReferencedContent(") .append(pk.getId()).append(", ").append(pk.getVersion()).append("))"); } else if ("InlineReference".equals(dataType)) { // ignore for now, doesn't work paerly as of yet } else if ("Binary".equals(dataType)) { // TODO: impl! // uses a new BinaryDescriptor( .. ) } } // "SIMPLE" dataTYPES if (DATATYPESSIMPLE.keySet().contains(dataType)) { for (String d : DATATYPESSIMPLE.keySet()) { if (d.equals(dataType)) { out.append(DATATYPESSIMPLE.get(d)).append("(").append(multiLang).append(", "); if (d.equals("Float") || d.equals("Double") || d.equals("LargeNumber") || d.equals("Boolean")) { out.append(defaultValue); } else { out.append("\"").append(defaultValue).append("\""); } out.append(")"); } } } out.trimToSize(); sopts.put("defaultValue", out.toString() + ","); } } // append options to script if (sopts.size() > 0) { 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) // remove last "," if written script.delete(script.length() - 1, script.length()); if (createProp) script.append(")\n"); // closing parenthesis return script.toString(); }