Changeset 526

Show
Ignore:
Timestamp:
08/11/08 04:45:14 (3 months ago)
Author:
jharrop
Message:

unmarshallFromTemplate method

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/XmlUtils.java

    r495 r526  
    560560         
    561561    } 
     562     
     563    
     564     /** 
     565      *  Give a string of wml containing ${key1}, ${key2}, return 
     566      *  a suitable object. 
     567     * @param wmlTemplateString 
     568     * @param mappings 
     569     * @return 
     570     */ 
     571    public static Object unmarshallFromTemplate(String wmlTemplateString, java.util.HashMap<String, String> mappings) { 
     572        String wmlString = replace(wmlTemplateString, 0, new StringBuilder(), mappings).toString(); 
     573        log.debug("Results of substitution: " + wmlString); 
     574        return unmarshalString(wmlString); 
     575     } 
     576 
     577     private static StringBuilder replace(String s, int offset, StringBuilder b, java.util.HashMap<String, String> mappings) { 
     578        int startKey = s.indexOf("${", offset); 
     579        if (startKey == -1) 
     580           return b.append(s.substring(offset)); 
     581        else { 
     582           b.append(s.substring(offset, startKey)); 
     583           int keyEnd = s.indexOf('}', startKey); 
     584           String key = s.substring(startKey + 2, keyEnd); 
     585           b.append( mappings.get(key) ); 
     586           return replace(s, keyEnd + 1, b, mappings); 
     587        } 
     588     } 
     589 
    562590         
    563591