@Test public void testCorrectHandlingOfEmptyPlaceholderText() { Map<Integer, PHData> phdata = new HashMap<>(); PHData bad = new PHData(Type.STANDALONE, "foo", "<foo/>", ""); bad.wsid = 1; phdata.put(1, bad); String result = WSPlaceholderUtil.restorePlaceholders("The quick brown fox jumps over <foo/>.", phdata); assertEquals("{1}The quick brown fox jumps over <foo/>.", result); }
public static String restorePlaceholders( String s, Map<Integer, PHData> phmap ) { if ( phmap.isEmpty() ) return removeEndTags( s ); StringBuilder sb = new StringBuilder( s ); StringBuilder missingPhSb = new StringBuilder(); for ( Map.Entry<Integer, PHData> e : phmap.entrySet() ) { PHData phData = e.getValue(); String ph = phData.toWSPlaceholder(); String tag = phData.mtForm; if (countOccurrences(sb, tag) > 1 && phData.correspondingOpenTag != null) { String openPh = phData.correspondingOpenTag.toWSPlaceholder(); int i = sb.indexOf(openPh); if (i != 1) { i = sb.indexOf(tag, i); if (i != -1 ) { sb.replace( i, i + tag.length(), ph ); continue; } } } int i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } else if ( tag.endsWith( "/>" ) ) { tag = tag.substring( 0, tag.length() - 2 ) + ">"; i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } } else if (tag.startsWith("<?") && tag.endsWith("?>")) { tag = tag.substring( 0, tag.length() - 2 ) + ">"; i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } } else { log.warn( String.format( "Tag %s (for %s) was not found in translation \"%s\". It will be appended at the end.", tag, ph, s ) ); missingPhSb.append( ph ); } } if ( missingPhSb.length() > 0 ) { sb.append( missingPhSb ); } return removeEndTags( sb.toString() ); }
WSPlaceholderUtil { public static String restorePlaceholders( String s, Map<Integer, PHData> phmap ) { if ( phmap.isEmpty() ) return removeEndTags( s ); StringBuilder sb = new StringBuilder( s ); StringBuilder missingPhSb = new StringBuilder(); for ( Map.Entry<Integer, PHData> e : phmap.entrySet() ) { PHData phData = e.getValue(); String ph = phData.toWSPlaceholder(); String tag = phData.mtForm; if (countOccurrences(sb, tag) > 1 && phData.correspondingOpenTag != null) { String openPh = phData.correspondingOpenTag.toWSPlaceholder(); int i = sb.indexOf(openPh); if (i != 1) { i = sb.indexOf(tag, i); if (i != -1 ) { sb.replace( i, i + tag.length(), ph ); continue; } } } int i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } else if ( tag.endsWith( "/>" ) ) { tag = tag.substring( 0, tag.length() - 2 ) + ">"; i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } } else if (tag.startsWith("<?") && tag.endsWith("?>")) { tag = tag.substring( 0, tag.length() - 2 ) + ">"; i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } } else { log.warn( String.format( "Tag %s (for %s) was not found in translation \"%s\". It will be appended at the end.", tag, ph, s ) ); missingPhSb.append( ph ); } } if ( missingPhSb.length() > 0 ) { sb.append( missingPhSb ); } return removeEndTags( sb.toString() ); } }
WSPlaceholderUtil { public static String restorePlaceholders( String s, Map<Integer, PHData> phmap ) { if ( phmap.isEmpty() ) return removeEndTags( s ); StringBuilder sb = new StringBuilder( s ); StringBuilder missingPhSb = new StringBuilder(); for ( Map.Entry<Integer, PHData> e : phmap.entrySet() ) { PHData phData = e.getValue(); String ph = phData.toWSPlaceholder(); String tag = phData.mtForm; if (countOccurrences(sb, tag) > 1 && phData.correspondingOpenTag != null) { String openPh = phData.correspondingOpenTag.toWSPlaceholder(); int i = sb.indexOf(openPh); if (i != 1) { i = sb.indexOf(tag, i); if (i != -1 ) { sb.replace( i, i + tag.length(), ph ); continue; } } } int i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } else if ( tag.endsWith( "/>" ) ) { tag = tag.substring( 0, tag.length() - 2 ) + ">"; i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } } else if (tag.startsWith("<?") && tag.endsWith("?>")) { tag = tag.substring( 0, tag.length() - 2 ) + ">"; i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } } else { log.warn( String.format( "Tag %s (for %s) was not found in translation \"%s\". It will be appended at the end.", tag, ph, s ) ); missingPhSb.append( ph ); } } if ( missingPhSb.length() > 0 ) { sb.append( missingPhSb ); } return removeEndTags( sb.toString() ); }  }
WSPlaceholderUtil { public static String restorePlaceholders( String s, Map<Integer, PHData> phmap ) { if ( phmap.isEmpty() ) return removeEndTags( s ); StringBuilder sb = new StringBuilder( s ); StringBuilder missingPhSb = new StringBuilder(); for ( Map.Entry<Integer, PHData> e : phmap.entrySet() ) { PHData phData = e.getValue(); String ph = phData.toWSPlaceholder(); String tag = phData.mtForm; if (countOccurrences(sb, tag) > 1 && phData.correspondingOpenTag != null) { String openPh = phData.correspondingOpenTag.toWSPlaceholder(); int i = sb.indexOf(openPh); if (i != 1) { i = sb.indexOf(tag, i); if (i != -1 ) { sb.replace( i, i + tag.length(), ph ); continue; } } } int i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } else if ( tag.endsWith( "/>" ) ) { tag = tag.substring( 0, tag.length() - 2 ) + ">"; i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } } else if (tag.startsWith("<?") && tag.endsWith("?>")) { tag = tag.substring( 0, tag.length() - 2 ) + ">"; i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } } else { log.warn( String.format( "Tag %s (for %s) was not found in translation \"%s\". It will be appended at the end.", tag, ph, s ) ); missingPhSb.append( ph ); } } if ( missingPhSb.length() > 0 ) { sb.append( missingPhSb ); } return removeEndTags( sb.toString() ); }  static String replacePlaceholders( String s, Map<Integer, PHData> phmap ); static Map<Integer, PHData> makePlaceholderMap( WSTextSegmentPlaceholder[] holders ); static String restorePlaceholders( String s, Map<Integer, PHData> phmap ); static String findEndTagsAtBeginning( String s ); static String removeEndTags( String s ); }
WSPlaceholderUtil { public static String restorePlaceholders( String s, Map<Integer, PHData> phmap ) { if ( phmap.isEmpty() ) return removeEndTags( s ); StringBuilder sb = new StringBuilder( s ); StringBuilder missingPhSb = new StringBuilder(); for ( Map.Entry<Integer, PHData> e : phmap.entrySet() ) { PHData phData = e.getValue(); String ph = phData.toWSPlaceholder(); String tag = phData.mtForm; if (countOccurrences(sb, tag) > 1 && phData.correspondingOpenTag != null) { String openPh = phData.correspondingOpenTag.toWSPlaceholder(); int i = sb.indexOf(openPh); if (i != 1) { i = sb.indexOf(tag, i); if (i != -1 ) { sb.replace( i, i + tag.length(), ph ); continue; } } } int i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } else if ( tag.endsWith( "/>" ) ) { tag = tag.substring( 0, tag.length() - 2 ) + ">"; i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } } else if (tag.startsWith("<?") && tag.endsWith("?>")) { tag = tag.substring( 0, tag.length() - 2 ) + ">"; i = sb.indexOf( tag ); if ( i >= 0 ) { sb.replace( i, i + tag.length(), ph ); } } else { log.warn( String.format( "Tag %s (for %s) was not found in translation \"%s\". It will be appended at the end.", tag, ph, s ) ); missingPhSb.append( ph ); } } if ( missingPhSb.length() > 0 ) { sb.append( missingPhSb ); } return removeEndTags( sb.toString() ); }  static String replacePlaceholders( String s, Map<Integer, PHData> phmap ); static Map<Integer, PHData> makePlaceholderMap( WSTextSegmentPlaceholder[] holders ); static String restorePlaceholders( String s, Map<Integer, PHData> phmap ); static String findEndTagsAtBeginning( String s ); static String removeEndTags( String s );  }