Skip to content

Commit

Permalink
add method to query fragment names in prepared template
Browse files Browse the repository at this point in the history
  • Loading branch information
erdos committed Jun 5, 2019
1 parent 2d8e39d commit 5df8ea8
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 18 deletions.
14 changes: 10 additions & 4 deletions java-src/io/github/erdos/stencil/TemplateVariables.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@
*/
public final class TemplateVariables {

private final Set<String> fragmentNames;
private final Set<String> variables;
private final Node root;

private TemplateVariables(Set<String> variables) {
private TemplateVariables(Set<String> variables, Set<String> fragmentNames) {
this.variables = unmodifiableSet(variables);
this.fragmentNames = unmodifiableSet(fragmentNames);

Node r = LEAF;
for (String x : variables) {
Expand Down Expand Up @@ -112,8 +114,8 @@ public Node visitLeaf() {
}
}

public static TemplateVariables fromPaths(Collection<String> allVariablePaths) {
return new TemplateVariables(new HashSet<>(allVariablePaths));
public static TemplateVariables fromPaths(Collection<String> allVariablePaths, Collection<String> allFragmentNames) {
return new TemplateVariables(new HashSet<>(allVariablePaths), new HashSet<>(allFragmentNames));
}

/**
Expand All @@ -124,8 +126,12 @@ public Set<String> getAllVariables() {
return variables;
}

public Set<String> getAllFragmentNames() {
return fragmentNames;
}

/**
* Throws IllegalArgumentException exception when template data.
* Throws IllegalArgumentException exception when template data is missing values for schema.
* <p>
* Throws when referred keys are missing from data.
*
Expand Down
14 changes: 10 additions & 4 deletions java-src/io/github/erdos/stencil/impl/NativeTemplateFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,15 @@ public PreparedFragment prepareFragmentFile(final File fragmentFile) throws IOEx
*/
@SuppressWarnings("unchecked")
private Set variableNames(Map prepared) {
// TODO: ez mindig null lesz ugyhogy csinaljunk vele valamit!!!!!
return prepared.containsKey(ClojureHelper.Keywords.VARIABLES)
? unmodifiableSet(new HashSet<Set>((Collection) prepared.get(ClojureHelper.Keywords.VARIABLES)))
return prepared.containsKey(ClojureHelper.Keywords.VARIABLES.kw)
? unmodifiableSet(new HashSet<Set>((Collection) prepared.get(ClojureHelper.Keywords.VARIABLES.kw)))
: emptySet();
}

@SuppressWarnings("unchecked")
private Set fragmentNames(Map prepared) {
return prepared.containsKey(ClojureHelper.Keywords.FRAGMENTS.kw)
? unmodifiableSet(new HashSet<Set>((Collection) prepared.get(ClojureHelper.Keywords.FRAGMENTS.kw)))
: emptySet();
}

Expand All @@ -92,7 +98,7 @@ private PreparedTemplate prepareTemplateImpl(TemplateDocumentFormats templateDoc
throw ParsingException.wrapping("Could not parse template file!", e);
}

final TemplateVariables vars = TemplateVariables.fromPaths(variableNames(prepared));
final TemplateVariables vars = TemplateVariables.fromPaths(variableNames(prepared), fragmentNames(prepared));

final File zipDirResource = (File) prepared.get(ClojureHelper.Keywords.SOURCE_FOLDER.kw);
if (zipDirResource != null) {
Expand Down
12 changes: 6 additions & 6 deletions java-test/io/github/erdos/stencil/TemplateVariablesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void testSimple() {
final Set<String> schema = set("a.x.p", "a.x.q");
Map<String, Object> data = map("a", map("x", map("p", null, "q", 23)));

fromPaths(schema).throwWhenInvalid(fromMap(data));
fromPaths(schema, emptySet()).throwWhenInvalid(fromMap(data));
}


Expand All @@ -30,12 +30,12 @@ public void testArray() {

// valid
final Map<String, Object> data = singletonMap("x", asList(1, 2, 3));
fromPaths(schema).throwWhenInvalid(fromMap(data));
fromPaths(schema, emptySet()).throwWhenInvalid(fromMap(data));

try {
// invalid
final TemplateData data2 = fromMap(singletonMap("a", singletonMap("x", asList(1, 2, 3))));
fromPaths(schema).throwWhenInvalid(data2);
fromPaths(schema, emptySet()).throwWhenInvalid(data2);
fail("Should have thrown!");
} catch (IllegalArgumentException ignored) {
}
Expand All @@ -47,11 +47,11 @@ public void testNestedArray() {

// valid
final Map<String, Object> data = singletonMap("a", singletonMap("x", singletonList(singletonList(singletonMap("p", 1)))));
fromPaths(schema).throwWhenInvalid(fromMap(data));
fromPaths(schema, emptySet()).throwWhenInvalid(fromMap(data));

try {
// invalid
fromPaths(schema).throwWhenInvalid(fromMap(singletonMap("a", singletonMap("x", asList(1, 2, 3)))));
fromPaths(schema, emptySet()).throwWhenInvalid(fromMap(singletonMap("a", singletonMap("x", asList(1, 2, 3)))));
fail("Should have thrown!");
} catch (IllegalArgumentException ignored) {
}
Expand All @@ -64,7 +64,7 @@ public void testNestedArraySimple() {
try {
// invalid
final Map<String, Object> data = singletonMap("a", 3);
fromPaths(schema).throwWhenInvalid(fromMap(data));
fromPaths(schema, emptySet()).throwWhenInvalid(fromMap(data));
fail("Should have thrown!");
} catch (IllegalArgumentException ignored) {
}
Expand Down
8 changes: 7 additions & 1 deletion src/stencil/cleanup.clj
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,18 @@
[]))]
(distinct (collect {} control-ast))))

; (find-variables [])
(defn- find-fragments [control-ast]
;; returns a set of fragment names use in this document
(set (for [item (tree-seq map? (comp flatten :blocks) {:blocks [control-ast]})
:when (map? item)
:when (= :cmd/include (:cmd item))]
(:name item))))

(defn process [raw-token-seq]
(let [ast (tokens->ast raw-token-seq)
executable (control-ast-normalize (annotate-environments ast))]
{:variables (find-variables ast)
:fragments (find-fragments ast)
:dynamic? (boolean (some :cmd executable))
:executable executable}))

Expand Down
2 changes: 1 addition & 1 deletion src/stencil/model.clj
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
(with-open [stream (io/input-stream xml-streamable)]
(-> (tokenizer/parse-to-tokens-seq stream)
(cleanup/process)
(select-keys [:variables :dynamic? :executable]))))
(select-keys [:variables :dynamic? :executable :fragments]))))


(defn load-template-model [^File dir]
Expand Down
22 changes: 21 additions & 1 deletion src/stencil/process.clj
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,32 @@

(set! *warn-on-reflection* true)

;; merge a set of fragment names under the :fragments key
(defn- merge-fragment-names [model]
(assoc model
:fragments
(-> #{}
(into (-> model :main :executable :fragments))
(into (for [x (:headers+footers (:main model))
f (:fragments (:executable x))] f)))))

(defn- merge-variable-names [model]
(assoc model
:variables
(-> #{}
(into (-> model :main :executable :variables))
(into (for [x (:headers+footers (:main model))
v (:variables (:executable x))] v)))))

(defn prepare-template [^InputStream stream]
(assert (instance? InputStream stream))
(let [zip-dir (FileHelper/createNonexistentTempFile "stencil-" ".zip.contents")]
(with-open [zip-stream stream] ;; FIXME: maybe not deleted immediately
(ZipHelper/unzipStreamIntoDirectory zip-stream zip-dir))
(model/load-template-model zip-dir)))
(-> zip-dir
model/load-template-model
merge-fragment-names
merge-variable-names)))

(defn prepare-fragment [input]
(assert (some? input))
Expand Down
9 changes: 8 additions & 1 deletion test/stencil/api_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,15 @@
(render-template-2 "/tmp/output-7.pptx"
{"customerName" "John Doe" "x" "XXX" "y" "yyyy"}))

(comment
(deftest test-prepare-fragment-names
(with-open [template (prepare "test-resources/multipart/main.docx")]
(is (= #{"header" "footer" "body"} (-> template .getVariables .getAllFragmentNames)))))

(deftest test-prepare-variable-names
(with-open [template (prepare "test-resources/multipart/header.docx")]
(is (= #{"name"} (-> template .getVariables .getAllVariables)))))

(comment

(let [template (prepare "test-resources/multipart/main.docx")
body (fragment "test-resources/multipart/body.docx")
Expand Down
1 change: 1 addition & 0 deletions test/stencil/process_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,6 @@
{:blocks [], :cmd :cmd/include, :name "elefant"}
{:close :b}
{:close :a}],
:fragments #{"elefant"}
:variables ()}
(test-prepare "<a><b>{%include \"elefant\"%}</b></a>")))))

0 comments on commit 5df8ea8

Please sign in to comment.