Skip to content

Commit

Permalink
add docx custom properties
Browse files Browse the repository at this point in the history
  • Loading branch information
vsch committed Apr 19, 2020
1 parent 1e7709c commit d2d4712
Show file tree
Hide file tree
Showing 7 changed files with 2,030 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions VERSION-TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,8 @@ Please give feedback on the upcoming changes if you have concerns about breaking

* Fix: add range limit to `Escaping.replaceAll(Pattern, BasedSequence, List<Range>, Replacer,
ReplacedTextMapper)` when setting matcher range.
* Add: `DocxRenderer.CUSTOM_PROPERTIES`, default `Collections.emptyMap()`, if present then
custom properties of the document will be set.

## 0.61.14

Expand Down
8 changes: 8 additions & 0 deletions VERSION.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- [Release 0.60.0](#release-0600)
- [API Refactoring](#api-refactoring)
- [0.61.16](#06116)
- [0.61.14](#06114)
- [0.61.12](#06112)
- [0.61.10](#06110)
Expand Down Expand Up @@ -163,6 +164,13 @@ Please give feedback on the upcoming changes if you have concerns about breaking
* `com.vladsch.flexmark.util.ast.NodeAdaptingVisitHandler`
* `com.vladsch.flexmark.util.ast.NodeAdaptingVisitor`

## 0.61.16

* Fix: add range limit to `Escaping.replaceAll(Pattern, BasedSequence, List<Range>, Replacer,
ReplacedTextMapper)` when setting matcher range.
* Add: `DocxRenderer.CUSTOM_PROPERTIES`, default `Collections.emptyMap()`, if present then
custom properties of the document will be set.

## 0.61.14

* Fix: [#398, Autolinks get cut off if they contain \`&amp;\` (escaped query params)] for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import com.vladsch.flexmark.util.sequence.Escaping;
import org.docx4j.Docx4J;
import org.docx4j.XmlUtils;
import org.docx4j.docProps.custom.Properties;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.DocPropsCustomPart;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.openpackaging.parts.WordprocessingML.NumberingDefinitionsPart;
import org.docx4j.openpackaging.parts.WordprocessingML.StyleDefinitionsPart;
Expand Down Expand Up @@ -147,6 +149,7 @@ public class DocxRenderer implements IRender {
// kludge it to use our resources
return EMOJI_RESOURCE_PREFIX;
});
final public static DataKey<Map<String, String>> CUSTOM_PROPERTIES = new DataKey<>("CUSTOM_PROPERTIES", Collections.emptyMap());

final List<NodeDocxRendererFactory> nodeFormatterFactories;
//final DocxRendererOptions rendererOptions;
Expand Down Expand Up @@ -227,6 +230,7 @@ static void setDefaultStyleAndNumbering(WordprocessingMLPackage out, DataHolder
out.addTargetPart(documentPart);
} catch (InvalidFormatException e) {
e.printStackTrace();
throw e;
}
}

Expand Down Expand Up @@ -254,6 +258,35 @@ static void setDefaultStyleAndNumbering(WordprocessingMLPackage out, DataHolder
}
}

public static void setDocumentProperties(WordprocessingMLPackage out, DataHolder options) {
Map<String, String> properties = CUSTOM_PROPERTIES.get(options);

if (!properties.isEmpty()) {
try {
DocPropsCustomPart customPropsPart = out.getDocPropsCustomPart();
if (customPropsPart == null) {
try {
customPropsPart = new DocPropsCustomPart();
out.addTargetPart(customPropsPart);
} catch (InvalidFormatException e) {
e.printStackTrace();
throw e;
}
}

if (customPropsPart.getContents() == null) {
customPropsPart.setContents(new Properties());
}

for (Map.Entry<String, String> entry : properties.entrySet()) {
customPropsPart.setProperty(entry.getKey(), entry.getValue());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* Render a node to the given word processing package
*
Expand All @@ -263,14 +296,15 @@ static void setDefaultStyleAndNumbering(WordprocessingMLPackage out, DataHolder
public void render(Node node, WordprocessingMLPackage output) {
DocxRenderer.MainDocxRenderer renderer = new DocxRenderer.MainDocxRenderer(options, output, node.getDocument(), null);
renderer.render(node);
setDocumentProperties(output, node.getDocument());
}

/**
* Render a node to the given word processing package
*
* @param node node to render
* @param output appendable to use for the output
* @param contentContainer contaner for content to use
* @param contentContainer container for content to use
*/
public void render(Node node, WordprocessingMLPackage output, DocumentContentHandler contentContainer) {
DocxRenderer.MainDocxRenderer renderer = new DocxRenderer.MainDocxRenderer(options, output, node.getDocument(), contentContainer);
Expand All @@ -296,6 +330,7 @@ public String render(@NotNull Node document) {
String resourcePath = DEFAULT_TEMPLATE_RESOURCE.get(getOptions());
WordprocessingMLPackage mlPackage = getDefaultTemplate(resourcePath);
render(document, mlPackage);
setDocumentProperties(mlPackage, document.getDocument());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
mlPackage.save(outputStream, Docx4J.FLAG_SAVE_FLAT_XML);
Expand Down Expand Up @@ -730,7 +765,7 @@ final public Iterable<? extends Node> reversedNodesOfType(Collection<Class<?>> c
@NotNull
@Override
public ResolvedLink resolveLink(@NotNull LinkType linkType, @NotNull CharSequence url, Boolean urlEncode) {
return resolveLink(linkType, url, (Attributes) null, urlEncode);
return resolveLink(linkType, url, null, urlEncode);
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public abstract class ComboDocxConverterSpecTestBase extends ComboSpecTestCase {
optionsMap.put("yellow-missing-hyperlink", new MutableDataSet().set(DocxRenderer.LOCAL_HYPERLINK_MISSING_HIGHLIGHT, "yellow"));
optionsMap.put("form-controls-input", new MutableDataSet().set(DocxRenderer.FORM_CONTROLS, "input"));
optionsMap.put("form-controls-form", new MutableDataSet().set(DocxRenderer.FORM_CONTROLS, "form"));

HashMap<String, String> props = new HashMap<>();
props.put("File Name", "TestFile");
props.put("Company", "Test Company Name");
optionsMap.put("properties", new MutableDataSet().set(DocxRenderer.CUSTOM_PROPERTIES, props));
}
private static String removeFileUri(String uri) {
if (uri.startsWith("file://")) return uri.substring("file://".length());
Expand Down
Loading

0 comments on commit d2d4712

Please sign in to comment.