Revision 5552c0c6
Added by Andreas Müller almost 2 years ago
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/word/out/WordExport.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2017 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.io.descriptive.word.out; |
|
10 |
|
|
11 |
import java.io.ByteArrayOutputStream; |
|
12 |
import java.io.File; |
|
13 |
import java.io.InputStream; |
|
14 |
|
|
15 |
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|
16 |
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|
17 |
import org.springframework.stereotype.Component; |
|
18 |
import org.springframework.transaction.TransactionStatus; |
|
19 |
|
|
20 |
import eu.etaxonomy.cdm.io.common.CdmExportBase; |
|
21 |
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer; |
|
22 |
import eu.etaxonomy.cdm.model.description.Feature; |
|
23 |
import eu.etaxonomy.cdm.model.term.DefinedTermBase; |
|
24 |
import eu.etaxonomy.cdm.model.term.TermNode; |
|
25 |
import eu.etaxonomy.cdm.model.term.TermTree; |
|
26 |
|
|
27 |
/** |
|
28 |
* @author pplitzner |
|
29 |
* @since Oct 18, 2018 |
|
30 |
*/ |
|
31 |
@Component |
|
32 |
public class WordExport extends CdmExportBase<WordExportConfigurator, WordExportState, IExportTransformer, File> { |
|
33 |
|
|
34 |
private static final long serialVersionUID = 3197379920692366008L; |
|
35 |
|
|
36 |
@Override |
|
37 |
protected boolean doCheck(WordExportState state) { |
|
38 |
return false; |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
protected void doInvoke(WordExportState state) { |
|
43 |
|
|
44 |
TransactionStatus txStatus = startTransaction(true); |
|
45 |
|
|
46 |
@SuppressWarnings("unchecked") |
|
47 |
TermTree<Feature> featureTree = state.getConfig().getFeatureTree(); |
|
48 |
featureTree = getTermTreeService().load(featureTree.getUuid()); |
|
49 |
TermNode<Feature> rootNode = featureTree.getRoot(); |
|
50 |
|
|
51 |
try { |
|
52 |
exportStream = generateDocx4JDocument(rootNode); |
|
53 |
state.getResult().addExportData(getByteArray()); |
|
54 |
} catch (Exception e) { |
|
55 |
e.printStackTrace(); |
|
56 |
} |
|
57 |
|
|
58 |
commitTransaction(txStatus); |
|
59 |
return; |
|
60 |
} |
|
61 |
|
|
62 |
private ByteArrayOutputStream generateDocx4JDocument(TermNode<?> rootNode) throws Exception { |
|
63 |
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("eu/etaxonomy/cdm/io/word/out/template.docx"); |
|
64 |
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(resourceAsStream); |
|
65 |
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart(); |
|
66 |
|
|
67 |
|
|
68 |
addChildNode(rootNode, mainDocumentPart, 1); |
|
69 |
|
|
70 |
// createTOC(mainDocumentPart); |
|
71 |
|
|
72 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
73 |
wordPackage.save(out); |
|
74 |
return out; |
|
75 |
} |
|
76 |
|
|
77 |
// public void createTOC(MainDocumentPart mainDocumentPart) throws Exception { |
|
78 |
// |
|
79 |
// ObjectFactory factory = Context.getWmlObjectFactory(); |
|
80 |
// |
|
81 |
// |
|
82 |
// P p = factory.createP(); |
|
83 |
// R r = factory.createR(); |
|
84 |
// |
|
85 |
// FldChar fldchar = factory.createFldChar(); |
|
86 |
// fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN); |
|
87 |
// r.getContent().add(getWrappedFldChar(fldchar)); |
|
88 |
// p.getContent().add(r); |
|
89 |
// |
|
90 |
// R r1 = factory.createR(); |
|
91 |
// Text txt = new Text(); |
|
92 |
// txt.setSpace("preserve"); |
|
93 |
// txt.setValue("TOC \\o \"1-3\" \\h \\z \\u \\h"); |
|
94 |
// r.getContent().add(factory.createRInstrText(txt) ); |
|
95 |
// p.getContent().add(r1); |
|
96 |
// |
|
97 |
// FldChar fldcharend = factory.createFldChar(); |
|
98 |
// fldcharend.setFldCharType(org.docx4j.wml.STFldCharType.END); |
|
99 |
// R r2 = factory.createR(); |
|
100 |
// r2.getContent().add(getWrappedFldChar(fldcharend)); |
|
101 |
// p.getContent().add(r2); |
|
102 |
// |
|
103 |
// mainDocumentPart.addObject(p); |
|
104 |
// |
|
105 |
// } |
|
106 |
// |
|
107 |
// @SuppressWarnings("unchecked") |
|
108 |
// public JAXBElement getWrappedFldChar(FldChar fldchar) { |
|
109 |
// return new JAXBElement( new QName(Namespaces.NS_WORD12, "fldChar"), org.docx4j.wml.FldChar.class, fldchar); |
|
110 |
// } |
|
111 |
|
|
112 |
private void addChildNode(TermNode<?> node, MainDocumentPart mainDocumentPart, int indent) throws Exception{ |
|
113 |
String styleId = "Heading"+indent; |
|
114 |
|
|
115 |
for (TermNode<?> childNode : node.getChildNodes()) { |
|
116 |
DefinedTermBase<?> term = childNode.getTerm(); |
|
117 |
mainDocumentPart.addStyledParagraphOfText(styleId, term.getLabel()); |
|
118 |
if(term.getDescription()!=null){ |
|
119 |
mainDocumentPart.addParagraphOfText("Description: "+term.getDescription()); |
|
120 |
} |
|
121 |
if(term.getUri()!=null){ |
|
122 |
mainDocumentPart.addParagraphOfText("URI: "+term.getUri().toString()); |
|
123 |
} |
|
124 |
addChildNode(childNode, mainDocumentPart, indent+1); |
|
125 |
} |
|
126 |
|
|
127 |
} |
|
128 |
|
|
129 |
@Override |
|
130 |
protected boolean isIgnore(WordExportState state) { |
|
131 |
return false; |
|
132 |
} |
|
133 |
|
|
134 |
} |
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/word/out/WordExportConfigurator.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2007 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
|
|
10 |
package eu.etaxonomy.cdm.io.descriptive.word.out; |
|
11 |
|
|
12 |
import java.io.File; |
|
13 |
|
|
14 |
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger; |
|
15 |
|
|
16 |
import eu.etaxonomy.cdm.database.ICdmDataSource; |
|
17 |
import eu.etaxonomy.cdm.io.common.XmlExportConfiguratorBase; |
|
18 |
import eu.etaxonomy.cdm.model.term.TermTree; |
|
19 |
|
|
20 |
|
|
21 |
/** |
|
22 |
* |
|
23 |
* @author pplitzner |
|
24 |
* @since Oct 18, 2018 |
|
25 |
* |
|
26 |
*/ |
|
27 |
public class WordExportConfigurator extends XmlExportConfiguratorBase<WordExportState> { |
|
28 |
|
|
29 |
private static final long serialVersionUID = -4360021755279592592L; |
|
30 |
|
|
31 |
@SuppressWarnings("unused") |
|
32 |
private static final Logger logger = LogManager.getLogger(WordExportConfigurator.class); |
|
33 |
|
|
34 |
private TermTree featureTree; |
|
35 |
|
|
36 |
|
|
37 |
private WordExportConfigurator(ICdmDataSource source, File destinationFolder, TermTree featureTree) { |
|
38 |
super(destinationFolder, source, null); |
|
39 |
this.featureTree = featureTree; |
|
40 |
} |
|
41 |
|
|
42 |
public static WordExportConfigurator NewInstance(ICdmDataSource source, File destinationFolder, TermTree featureTree) { |
|
43 |
return new WordExportConfigurator(source, destinationFolder, featureTree); |
|
44 |
} |
|
45 |
|
|
46 |
@Override |
|
47 |
public WordExportState getNewState() { |
|
48 |
return new WordExportState(this); |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
protected void makeIoClassList() { |
|
53 |
ioClassList = new Class[] { |
|
54 |
WordExport.class |
|
55 |
}; |
|
56 |
} |
|
57 |
|
|
58 |
public TermTree getFeatureTree() { |
|
59 |
return featureTree; |
|
60 |
} |
|
61 |
|
|
62 |
} |
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/word/out/WordExportState.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2007 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.io.descriptive.word.out; |
|
10 |
|
|
11 |
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger; |
|
12 |
|
|
13 |
import eu.etaxonomy.cdm.io.common.XmlExportState; |
|
14 |
|
|
15 |
/** |
|
16 |
* |
|
17 |
* @author pplitzner |
|
18 |
* @since Oct 18, 2018 |
|
19 |
* |
|
20 |
*/ |
|
21 |
public class WordExportState extends XmlExportState<WordExportConfigurator>{ |
|
22 |
|
|
23 |
@SuppressWarnings("unused") |
|
24 |
private static final Logger logger = LogManager.getLogger(WordExportState.class); |
|
25 |
|
|
26 |
public WordExportState(WordExportConfigurator config) { |
|
27 |
super(config); |
|
28 |
} |
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
} |
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/word/out/WordTermExport.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2017 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.io.descriptive.word.out; |
|
10 |
|
|
11 |
import java.io.ByteArrayOutputStream; |
|
12 |
import java.io.File; |
|
13 |
import java.io.InputStream; |
|
14 |
|
|
15 |
import org.docx4j.openpackaging.packages.WordprocessingMLPackage; |
|
16 |
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; |
|
17 |
import org.springframework.stereotype.Component; |
|
18 |
import org.springframework.transaction.TransactionStatus; |
|
19 |
|
|
20 |
import eu.etaxonomy.cdm.io.common.CdmExportBase; |
|
21 |
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer; |
|
22 |
import eu.etaxonomy.cdm.model.description.Feature; |
|
23 |
import eu.etaxonomy.cdm.model.term.DefinedTermBase; |
|
24 |
import eu.etaxonomy.cdm.model.term.TermNode; |
|
25 |
import eu.etaxonomy.cdm.model.term.TermTree; |
|
26 |
|
|
27 |
/** |
|
28 |
* @author pplitzner |
|
29 |
* @since Oct 18, 2018 |
|
30 |
*/ |
|
31 |
@Component |
|
32 |
public class WordTermExport extends CdmExportBase<WordTermExportConfigurator, WordTermExportState, IExportTransformer, File> { |
|
33 |
|
|
34 |
private static final long serialVersionUID = 3197379920692366008L; |
|
35 |
|
|
36 |
@Override |
|
37 |
protected boolean doCheck(WordTermExportState state) { |
|
38 |
return false; |
|
39 |
} |
|
40 |
|
|
41 |
@Override |
|
42 |
protected void doInvoke(WordTermExportState state) { |
|
43 |
|
|
44 |
TransactionStatus txStatus = startTransaction(true); |
|
45 |
|
|
46 |
@SuppressWarnings("unchecked") |
|
47 |
TermTree<Feature> featureTree = state.getConfig().getFeatureTree(); |
|
48 |
featureTree = getTermTreeService().load(featureTree.getUuid()); |
|
49 |
TermNode<Feature> rootNode = featureTree.getRoot(); |
|
50 |
|
|
51 |
try { |
|
52 |
exportStream = generateDocx4JDocument(rootNode); |
|
53 |
state.getResult().addExportData(getByteArray()); |
|
54 |
} catch (Exception e) { |
|
55 |
e.printStackTrace(); |
|
56 |
} |
|
57 |
|
|
58 |
commitTransaction(txStatus); |
|
59 |
return; |
|
60 |
} |
|
61 |
|
|
62 |
private ByteArrayOutputStream generateDocx4JDocument(TermNode<?> rootNode) throws Exception { |
|
63 |
InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("eu/etaxonomy/cdm/io/word/out/template.docx"); |
|
64 |
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.load(resourceAsStream); |
|
65 |
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart(); |
|
66 |
|
|
67 |
|
|
68 |
addChildNode(rootNode, mainDocumentPart, 1); |
|
69 |
|
|
70 |
// createTOC(mainDocumentPart); |
|
71 |
|
|
72 |
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
|
73 |
wordPackage.save(out); |
|
74 |
return out; |
|
75 |
} |
|
76 |
|
|
77 |
// public void createTOC(MainDocumentPart mainDocumentPart) throws Exception { |
|
78 |
// |
|
79 |
// ObjectFactory factory = Context.getWmlObjectFactory(); |
|
80 |
// |
|
81 |
// |
|
82 |
// P p = factory.createP(); |
|
83 |
// R r = factory.createR(); |
|
84 |
// |
|
85 |
// FldChar fldchar = factory.createFldChar(); |
|
86 |
// fldchar.setFldCharType(org.docx4j.wml.STFldCharType.BEGIN); |
|
87 |
// r.getContent().add(getWrappedFldChar(fldchar)); |
|
88 |
// p.getContent().add(r); |
|
89 |
// |
|
90 |
// R r1 = factory.createR(); |
|
91 |
// Text txt = new Text(); |
|
92 |
// txt.setSpace("preserve"); |
|
93 |
// txt.setValue("TOC \\o \"1-3\" \\h \\z \\u \\h"); |
|
94 |
// r.getContent().add(factory.createRInstrText(txt) ); |
|
95 |
// p.getContent().add(r1); |
|
96 |
// |
|
97 |
// FldChar fldcharend = factory.createFldChar(); |
|
98 |
// fldcharend.setFldCharType(org.docx4j.wml.STFldCharType.END); |
|
99 |
// R r2 = factory.createR(); |
|
100 |
// r2.getContent().add(getWrappedFldChar(fldcharend)); |
|
101 |
// p.getContent().add(r2); |
|
102 |
// |
|
103 |
// mainDocumentPart.addObject(p); |
|
104 |
// |
|
105 |
// } |
|
106 |
// |
|
107 |
// @SuppressWarnings("unchecked") |
|
108 |
// public JAXBElement getWrappedFldChar(FldChar fldchar) { |
|
109 |
// return new JAXBElement( new QName(Namespaces.NS_WORD12, "fldChar"), org.docx4j.wml.FldChar.class, fldchar); |
|
110 |
// } |
|
111 |
|
|
112 |
private void addChildNode(TermNode<?> node, MainDocumentPart mainDocumentPart, int indent) throws Exception{ |
|
113 |
String styleId = "Heading"+indent; |
|
114 |
|
|
115 |
for (TermNode<?> childNode : node.getChildNodes()) { |
|
116 |
DefinedTermBase<?> term = childNode.getTerm(); |
|
117 |
mainDocumentPart.addStyledParagraphOfText(styleId, term.getLabel()); |
|
118 |
if(term.getDescription()!=null){ |
|
119 |
mainDocumentPart.addParagraphOfText("Description: "+term.getDescription()); |
|
120 |
} |
|
121 |
if(term.getUri()!=null){ |
|
122 |
mainDocumentPart.addParagraphOfText("URI: "+term.getUri().toString()); |
|
123 |
} |
|
124 |
addChildNode(childNode, mainDocumentPart, indent+1); |
|
125 |
} |
|
126 |
|
|
127 |
} |
|
128 |
|
|
129 |
@Override |
|
130 |
protected boolean isIgnore(WordTermExportState state) { |
|
131 |
return false; |
|
132 |
} |
|
133 |
|
|
134 |
} |
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/word/out/WordTermExportConfigurator.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2007 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
|
|
10 |
package eu.etaxonomy.cdm.io.descriptive.word.out; |
|
11 |
|
|
12 |
import java.io.File; |
|
13 |
|
|
14 |
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger; |
|
15 |
|
|
16 |
import eu.etaxonomy.cdm.database.ICdmDataSource; |
|
17 |
import eu.etaxonomy.cdm.io.common.XmlExportConfiguratorBase; |
|
18 |
import eu.etaxonomy.cdm.model.term.TermTree; |
|
19 |
|
|
20 |
|
|
21 |
/** |
|
22 |
* |
|
23 |
* @author pplitzner |
|
24 |
* @since Oct 18, 2018 |
|
25 |
* |
|
26 |
*/ |
|
27 |
public class WordTermExportConfigurator extends XmlExportConfiguratorBase<WordTermExportState> { |
|
28 |
|
|
29 |
private static final long serialVersionUID = -4360021755279592592L; |
|
30 |
|
|
31 |
@SuppressWarnings("unused") |
|
32 |
private static final Logger logger = LogManager.getLogger(WordTermExportConfigurator.class); |
|
33 |
|
|
34 |
private TermTree featureTree; |
|
35 |
|
|
36 |
|
|
37 |
private WordTermExportConfigurator(ICdmDataSource source, File destinationFolder, TermTree featureTree) { |
|
38 |
super(destinationFolder, source, null); |
|
39 |
this.featureTree = featureTree; |
|
40 |
} |
|
41 |
|
|
42 |
public static WordTermExportConfigurator NewInstance(ICdmDataSource source, File destinationFolder, TermTree featureTree) { |
|
43 |
return new WordTermExportConfigurator(source, destinationFolder, featureTree); |
|
44 |
} |
|
45 |
|
|
46 |
@Override |
|
47 |
public WordTermExportState getNewState() { |
|
48 |
return new WordTermExportState(this); |
|
49 |
} |
|
50 |
|
|
51 |
@Override |
|
52 |
protected void makeIoClassList() { |
|
53 |
ioClassList = new Class[] { |
|
54 |
WordTermExport.class |
|
55 |
}; |
|
56 |
} |
|
57 |
|
|
58 |
public TermTree getFeatureTree() { |
|
59 |
return featureTree; |
|
60 |
} |
|
61 |
|
|
62 |
} |
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/word/out/WordTermExportState.java | ||
---|---|---|
1 |
/** |
|
2 |
* Copyright (C) 2007 EDIT |
|
3 |
* European Distributed Institute of Taxonomy |
|
4 |
* http://www.e-taxonomy.eu |
|
5 |
* |
|
6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
|
7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
|
8 |
*/ |
|
9 |
package eu.etaxonomy.cdm.io.descriptive.word.out; |
|
10 |
|
|
11 |
import org.apache.logging.log4j.LogManager;import org.apache.logging.log4j.Logger; |
|
12 |
|
|
13 |
import eu.etaxonomy.cdm.io.common.XmlExportState; |
|
14 |
|
|
15 |
/** |
|
16 |
* |
|
17 |
* @author pplitzner |
|
18 |
* @since Oct 18, 2018 |
|
19 |
* |
|
20 |
*/ |
|
21 |
public class WordTermExportState extends XmlExportState<WordTermExportConfigurator>{ |
|
22 |
|
|
23 |
@SuppressWarnings("unused") |
|
24 |
private static final Logger logger = LogManager.getLogger(WordTermExportState.class); |
|
25 |
|
|
26 |
public WordTermExportState(WordTermExportConfigurator config) { |
|
27 |
super(config); |
|
28 |
} |
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
} |
Also available in: Unified diff
rename WordTermExport