Skip to content

Commit

Permalink
Fixed #150 When the header of the user content is missing the content…
Browse files Browse the repository at this point in the history
… is lost.
  • Loading branch information
ylussaud committed Feb 19, 2021
1 parent 6b52213 commit 18fc13a
Show file tree
Hide file tree
Showing 95 changed files with 364 additions and 5 deletions.
Expand Up @@ -38,6 +38,11 @@ public class GenerationResult {
*/
private ValidationMessageLevel level = ValidationMessageLevel.OK;

/**
* The lost document {@link URI} if the target document has parsing errors (user content).
*/
private URI lostDocumentURI;

/**
* Mapping of lost fragments from {@link UserDoc#getId() user doc ID} to fragment {@link URI}.
*/
Expand Down Expand Up @@ -81,6 +86,25 @@ public ValidationMessageLevel updateLevel(ValidationMessageLevel... levels) {
return level;
}

/**
* Gets the lost document URI.
*
* @return the lost document URI if the target document has parsing errors (user content), <code>null</code> otherwise
*/
public URI getLostDocumentURI() {
return lostDocumentURI;
}

/**
* Sets the lost document {@link URI}.
*
* @param lostDocumentURI
* the lost document {@link URI}
*/
public void setLostDocumentURI(URI lostDocumentURI) {
this.lostDocumentURI = lostDocumentURI;
}

/**
* Gets the mapping of lost fragments from {@link UserDoc#getId() user doc ID} to fragment {@link URI}.
*
Expand Down
Expand Up @@ -20,6 +20,7 @@
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
Expand All @@ -34,8 +35,10 @@
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.URIConverter;
import org.obeonetwork.m2doc.POIServices;
import org.obeonetwork.m2doc.parser.TemplateValidationMessage;
import org.obeonetwork.m2doc.parser.ValidationMessageLevel;
import org.obeonetwork.m2doc.template.DocumentTemplate;
import org.obeonetwork.m2doc.template.IConstruct;
import org.obeonetwork.m2doc.template.UserContent;
import org.obeonetwork.m2doc.util.M2DocUtils;
import org.obeonetwork.m2doc.util.MemoryURIHandler;
Expand Down Expand Up @@ -99,6 +102,11 @@ public class UserContentManager {
*/
private DocumentTemplate userDocDocument;

/**
* Tells if we should copy the source document to a lost document.
*/
private boolean copyToLostDocument;

/**
* Constructor.
*
Expand Down Expand Up @@ -133,6 +141,7 @@ private void launchParsing() {
if (memoryCopy != null) {
try {
userDocDocument = M2DocUtils.parseUserContent(uriConverter, memoryCopy, queryEnvironment);
copyToLostDocument = hasError(userDocDocument);
final TreeIterator<EObject> iter = userDocDocument.eAllContents();
while (iter.hasNext()) {
EObject eObject = iter.next();
Expand All @@ -154,6 +163,33 @@ private void launchParsing() {
}
}

/**
* Tells if the given {@link DocumentTemplate} has {@link ValidationMessageLevel#ERROR errors}.
*
* @param document
* the {@link DocumentTemplate}.
* @return <code>true</code> if the given {@link DocumentTemplate} has {@link ValidationMessageLevel#ERROR errors}, <code>false</code>
* oterhwise
*/
private boolean hasError(DocumentTemplate document) {
boolean res = false;

final Iterator<EObject> it = document.eAllContents();
end: while (it.hasNext()) {
final EObject eObj = it.next();
if (eObj instanceof IConstruct) {
for (TemplateValidationMessage message : ((IConstruct) eObj).getValidationMessages()) {
if (message.getLevel() == ValidationMessageLevel.ERROR) {
res = true;
break end;
}
}
}
}

return res;
}

/**
* Stores the given {@link UserContent} in the given {@link Map}.
*
Expand Down Expand Up @@ -298,6 +334,19 @@ public void generateLostFiles(GenerationResult result, RawCopier copier)
launchParsing();
}

try (InputStream is = uriConverter.createInputStream(sourceURI);
OPCPackage oPackage = OPCPackage.open(is);
XWPFDocument destinationDocument = new XWPFDocument(oPackage);) {
if (copyToLostDocument) {
final URI lostDocumentURI = getLostDocumentURI(destinationURI);
result.setLostDocumentURI(lostDocumentURI);
copy(destinationURI, lostDocumentURI);
XWPFParagraph currentGeneratedParagraph = destinationDocument.createParagraph();
result.addMessage(M2DocUtils.appendMessageRun(currentGeneratedParagraph, ValidationMessageLevel.WARNING,
"backup document created"));
}
}

for (Entry<String, List<UserContent>> entry : mapIdUserContent.entrySet()) {
final URI lostUserContentURI = getLostUserContentURI(destinationURI, entry.getKey());
result.getLostUserContents().put(entry.getKey(), lostUserContentURI);
Expand Down Expand Up @@ -359,4 +408,18 @@ protected URI getLostUserContentURI(URI dest, String id) {
return res.resolve(dest);
}

/**
* Gets the lost document {@link URI} for the given destination {@link URI}.
*
* @param dest
* the destination {@link URI}
* @return the lost document {@link URI} for the given destination {@link URI}
*/
protected URI getLostDocumentURI(URI dest) {
final String date = format.format(new Date()).replace("/", "_").replace(" ", "_");
final URI res = URI.createURI("./" + dest.lastSegment() + "-" + date + "-backup.docx", false);

return res.resolve(dest);
}

}
Expand Up @@ -169,8 +169,7 @@ private UserContent parseUserContent() throws DocumentParserException {
if (tagText == null || "".equals(tagText)) {
final XWPFRun lastRun = userContent.getRuns().get(userContent.getRuns().size() - 1);
TemplateValidationMessage templateValidationMessage = new TemplateValidationMessage(
ValidationMessageLevel.WARNING, ParsingErrorMessage.INVALID_USERCONTENT_VALUE.getMessage(),
lastRun);
ValidationMessageLevel.ERROR, ParsingErrorMessage.INVALID_USERCONTENT_VALUE.getMessage(), lastRun);
userContent.getValidationMessages().add(templateValidationMessage);
} else {
userContent.setId(tagText);
Expand Down
Expand Up @@ -707,17 +707,17 @@ public static DocumentTemplate parseUserContent(URIConverter uriConverter, URI d
final XWPFDocument document = new XWPFDocument(oPackage);
r.getContents().add(result);
final BodyGeneratedParser parser = new BodyGeneratedParser(document, queryEnvironment);
result.setBody(parser.parseBlock(null));
result.setBody(parser.parseBlock(null, TokenType.EOF));
result.setInputStream(is);
result.setOpcPackage(oPackage);
result.setDocument(document);
for (XWPFFooter footer : document.getFooterList()) {
final BodyGeneratedParser footerParser = new BodyGeneratedParser(footer, queryEnvironment);
result.getFooters().add(footerParser.parseBlock(null));
result.getFooters().add(footerParser.parseBlock(null, TokenType.EOF));
}
for (XWPFHeader header : document.getHeaderList()) {
final BodyGeneratedParser headerParser = new BodyGeneratedParser(header, queryEnvironment);
result.getHeaders().add(headerParser.parseBlock(null));
result.getHeaders().add(headerParser.parseBlock(null, TokenType.EOF));
}

} catch (IOException e) {
Expand Down
Binary file not shown.
@@ -0,0 +1 @@
WARNING - backup document created
@@ -0,0 +1,16 @@

=== HEADER ===

=== BODY ===

A simple demonstration of a user doc :

userdoc 'zone1' do

Some protected text.
enduserdoc

End of demonstration.
=== FOOTER ===

=== TEMPLATES ===
Binary file not shown.
@@ -0,0 +1 @@
WARNING - backup document created
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="missingEndUserContent" templateFileName="missingEndUserContent-template.docx"/>
@@ -0,0 +1,22 @@

=== HEADER ===




=== BODY ===


=== FOOTER ===



A simple demonstration of a user doc :

userdoc 'zone1' do

Some protected text.
enduserdoc

End of demonstration.
=== TEMPLATES ===
Binary file not shown.
@@ -0,0 +1 @@
WARNING - backup document created
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="missingEndUserContent" templateFileName="missingEndUserContent-template.docx"/>
@@ -0,0 +1,22 @@

=== HEADER ===



A simple demonstration of a user doc :

userdoc 'zone1' do

Some protected text.
enduserdoc

End of demonstration.
=== BODY ===


=== FOOTER ===




=== TEMPLATES ===
Binary file not shown.
@@ -0,0 +1 @@
WARNING - backup document created
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="missingEndUserContent" templateFileName="missingEndUserContent-template.docx"/>
@@ -0,0 +1,21 @@

=== HEADER ===




=== BODY ===


=== FOOTER ===



A simple demonstration of a user doc :

userdoc 'zone1' do

Some protected text.End of demonstration.
enduserdoc

=== TEMPLATES ===
@@ -0,0 +1 @@
ERROR - Invalid block: Unexpected tag EOF missing [ENDUSERDOC]
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="missingEndUserDoc" templateFileName="missingEndUserDoc-template.docx"/>
@@ -0,0 +1,21 @@

=== HEADER ===



A simple demonstration of a user doc :

userdoc 'zone1' do

Some protected text.End of demonstration.
enduserdoc

=== BODY ===


=== FOOTER ===




=== TEMPLATES ===
@@ -0,0 +1 @@
ERROR - Invalid block: Unexpected tag EOF missing [ENDUSERDOC]
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="missingEndUserDoc" templateFileName="missingEndUserDoc-template.docx"/>
@@ -0,0 +1,15 @@

=== HEADER ===

=== BODY ===

A simple demonstration of a user doc :

userdoc 'zone1' do

Some protected text.End of demonstration.
enduserdoc

=== FOOTER ===

=== TEMPLATES ===
@@ -0,0 +1 @@
ERROR - Invalid block: Unexpected tag EOF missing [ENDUSERDOC]
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="missingEndUserDoc" templateFileName="missingEndUserDoc-template.docx"/>
Binary file not shown.
@@ -0,0 +1 @@
WARNING - backup document created
Binary file not shown.
@@ -0,0 +1 @@
WARNING - backup document created
Binary file not shown.
@@ -0,0 +1 @@
WARNING - backup document created
@@ -0,0 +1,22 @@

=== HEADER ===




=== BODY ===


=== FOOTER ===



A simple demonstration of a user doc :

userdoc 'zone1' do

Some protected text.
enduserdoc

End of demonstration.
=== TEMPLATES ===
Binary file not shown.
@@ -0,0 +1 @@
WARNING - backup document created
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="ASCII"?>
<genconf:Generation xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:genconf="http://www.obeonetwork.org/m2doc/genconf/1.0" name="missingUserContentID" templateFileName="missingUserContentID-template.docx"/>

0 comments on commit 18fc13a

Please sign in to comment.