Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added a variableReplace with custom delimiters #203

Open
wants to merge 1 commit into
base: VERSION_3_3_0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/main/java/org/docx4j/XmlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,12 @@ public static Object unmarshallFromTemplate(String wmlTemplateString,
}


private static StringBuilder replace(String wmlTemplateString, int offset, StringBuilder strB,
public static Object unmarshallFromTemplate(String wmlTemplateString, java.util.Map<String, ?> mappings, JAXBContext jc, String startVar, String endVar) throws JAXBException {
String wmlString = replace(wmlTemplateString, 0, new StringBuilder(), mappings, startVar, endVar).toString();
return unmarshalString(wmlString, jc);
}

private static StringBuilder replace(String wmlTemplateString, int offset, StringBuilder strB,
java.util.Map<String, ?> mappings) {

int startKey = wmlTemplateString.indexOf("${", offset);
Expand All @@ -528,6 +533,27 @@ private static StringBuilder replace(String wmlTemplateString, int offset, Strin
}
}

private static StringBuilder replace(String wmlTemplateString, int offset, StringBuilder strB,
java.util.Map<String, ?> mappings, String startVar, String endVar) {

int startKey = wmlTemplateString.indexOf(startVar, offset);
if (startKey == -1)
return strB.append(wmlTemplateString.substring(offset));
else {
strB.append(wmlTemplateString.substring(offset, startKey));
int keyEnd = wmlTemplateString.indexOf(endVar, startKey);
String key = wmlTemplateString.substring(startKey + startVar.length(), keyEnd);
String val = mappings.get(key).toString();
if (val==null) {
log.warn("Invalid key '" + key + "' or key not mapped to a value");
strB.append(key);
} else {
strB.append(val);
}
return replace(wmlTemplateString, keyEnd + 1, strB, mappings, startVar, endVar);
}
}

/**
* Marshal this object to a String, pretty printed, and without an XML declaration.
* Useful for debugging.
Expand Down Expand Up @@ -1466,4 +1492,4 @@ public static void treeCopy( Node sourceNode, Node destParent ) {
}
}

}
}
39 changes: 38 additions & 1 deletion src/main/java/org/docx4j/openpackaging/parts/JaxbXmlPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,44 @@ public void variableReplace(java.util.HashMap<String, String> mappings) throws J
XmlUtils.unmarshallFromTemplate(wmlTemplateString, mappings, jc));

}


public void variableReplace(java.util.HashMap<String, String> mappings, String startVar, String endVar) throws JAXBException, Docx4JException {

// Get the contents as a string
String wmlTemplateString = null;
if (jaxbElement==null) {

PartStore partStore = this.getPackage().getSourcePartStore();
String name = this.getPartName().getName();
InputStream is = partStore.loadPart(
name.substring(1));
if (is==null) {
log.warn(name + " missing from part store");
throw new Docx4JException(name + " missing from part store");
} else {
log.info("Lazily unmarshalling " + name);
// wmlTemplateString = convertStreamToString(is);

// This seems to be about 5% faster than the Scanner approach
try {
wmlTemplateString = IOUtils.toString(is, "UTF-8");
} catch (IOException e) {
throw new Docx4JException(e.getMessage(), e);
}
}

} else {

wmlTemplateString = XmlUtils.marshaltoString(jaxbElement, true, false, jc);

}

// Do the replacement
jaxbElement = (E)XmlUtils.unwrap(
XmlUtils.unmarshallFromTemplate(wmlTemplateString, mappings, jc, startVar, endVar));

}

// private static String convertStreamToString(java.io.InputStream is) {
// java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
// return s.hasNext() ? s.next() : "";
Expand Down