Kogie / xdocreport

Automatically exported from code.google.com/p/xdocreport
1 stars 0 forks source link

xml parse exception when using SyntaxKind.NoEscape ; #213

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Running your example code  
"http://code.google.com/p/xdocreport/source/browse/samples/fr.opensagres.xdocrep
ort.samples.docxandvelocity/src/fr/opensagres/xdocreport/samples/docxandvelocity
/DocxTextStylingWithVelocity.java?repo=samples"
leads to following error : 
[Fatal Error] :1:6073: The prefix "w" for element "w:r" is not bound.
7-jan-2013 13:12:50 
fr.opensagres.xdocreport.document.textstyling.AbstractTextStylingTransformer 
transform
SEVERE: The prefix "w" for element "w:r" is not bound.
fr.opensagres.xdocreport.core.XDocReportException: 
org.xml.sax.SAXParseException: The prefix "w" for element "w:r" is not bound.
    at fr.opensagres.xdocreport.document.textstyling.AbstractTextStylingTransformer.transform(AbstractTextStylingTransformer.java:68)

What version of the product are you using? On what operating system?
1.0.0

Please provide any additional information below.

Original issue reported on code.google.com by mario.cl...@gmail.com on 7 Jan 2013 at 12:19

GoogleCodeExporter commented 9 years ago
Hi Mario,

I don't understand how you can have this exception. Are you sure that you have 
not modified the 
http://code.google.com/p/xdocreport/source/browse/samples/fr.opensagres.xdocrepo
rt.samples.docxandvelocity/src/fr/opensagres/xdocreport/samples/docxandvelocity/
DocxTextStylingWithVelocity.java?repo=samples ?

SyntaxKind.NoEscape should not use AbstractTextStylingTransformer. It's other 
syntax kind like html, GWiki etc whicu uses AbstractTextStylingTransformer.

Regards Angelo

Original comment by angelo.z...@gmail.com on 7 Jan 2013 at 5:33

GoogleCodeExporter commented 9 years ago
I hereby include my code : the strange thing is that it works when I add only 
"test" or "test2".  When I add both, I get the error mentioned earlier.

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.core.document.SyntaxKind;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.images.ClassPathImageProvider;
import fr.opensagres.xdocreport.document.images.IImageProvider;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
import javax.xml.parsers.SAXParserFactory;

public class HeaderFooterImageTest {

    public static void main(String[] args) {
        try {
            // 1) Load Docx file by filling Velocity template engine and cache
            // it to the registry
            InputStream in = HeaderFooterImageTest.class.getResourceAsStream("abcdef.docx");
            IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
                    in, TemplateEngineKind.Velocity);

            // 2) Create fields metadata to manage image
            FieldsMetadata metadata = report.createFieldsMetadata();
            metadata.addFieldAsImage("a");
            metadata.addFieldAsImage("b");
            metadata.addFieldAsImage("c");
            metadata.addFieldAsImage("d");
            metadata.addFieldAsImage("e");
            metadata.addFieldAsImage("f");
            metadata.addFieldAsTextStyling("test", SyntaxKind.Html);
            metadata.addFieldAsTextStyling("test2", SyntaxKind.NoEscape);

            report.setFieldsMetadata(metadata);

            // 3) Create context Java model
            IContext context = report.createContext();
            context.put("project", "project");
            IImageProvider a = new ClassPathImageProvider(HeaderFooterImageTest.class, "a.jpg");
            IImageProvider b = new ClassPathImageProvider(HeaderFooterImageTest.class, "b.jpg");
            IImageProvider c = new ClassPathImageProvider(HeaderFooterImageTest.class, "c.jpg");
            IImageProvider d = new ClassPathImageProvider(HeaderFooterImageTest.class, "d.jpg");
            IImageProvider e = new ClassPathImageProvider(HeaderFooterImageTest.class, "e.jpg");
            IImageProvider f = new ClassPathImageProvider(HeaderFooterImageTest.class, "f.png");

            context.put("a", a);
            context.put("b", b);
            context.put("c", c);
            context.put("d", d);
            context.put("e", e);
            context.put("f", f);
//            context.put("test", "<p>hello hello</p><p>world</p>");
            context.put("test", "hello hello<br/>world");

            context.put("test2", "<w:r>" + "<w:t xml:space=\"preserve\">Here a + </w:t>" + "</w:r>"
                    + "<w:r w:rsidRPr=\"0070620D\">" + "<w:rPr>" + "<w:b />" + "</w:rPr>" + "<w:t>bold</w:t>" + "</w:r>"
                    + "<w:r>" + "<w:t xml:space=\"preserve\"> text.</w:t>" + "</w:r>");

            // 4) Generate report by merging Java model with the Docx
            OutputStream out = new FileOutputStream(new File(
                    "abcdef_Out.docx"));

            report.process(context, out);
            System.out.println("done");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XDocReportException e) {
            e.printStackTrace();
        }
    }
}

Original comment by mario.cl...@gmail.com on 8 Jan 2013 at 12:50

Attachments:

GoogleCodeExporter commented 9 years ago
Hi Mario,

Many thank's to have attached your docx+Java.

Ok I understand more your problem. It's a big bug with XDocReport with 
FieldsMetadata.
The problem comes from that you use "test" and "test2" field name which starts 
with the same string "test" : 
When preprocessing is done, XDocReport loops for each text styling fields and 
test if "test styling" preprocessing must be done or not
when mergefield is found. In your case, when 

 - "test" mergefield is tested, FieldMetadata with "test" is found => HTML syntax
 - "test2" mergefield is tested, FieldMetadata with "test" is found => HTML syntax, but it's not correct.

With your sample, I think if you do :

-----------------------------------------------------------
metadata.addFieldAsTextStyling("test2", SyntaxKind.NoEscape);
metadata.addFieldAsTextStyling("test", SyntaxKind.Html);
-----------------------------------------------------------

it should work.

I will try to fix this problem by sorting fields by name.

Regards Angelo

Original comment by angelo.z...@gmail.com on 9 Jan 2013 at 8:07

GoogleCodeExporter commented 9 years ago
I have fixed the problem on 1.0.1. See Git commit 
http://code.google.com/p/xdocreport/source/detail?r=62235a0a0c15f9d3e1d4c4fc9b45
c7b4f076a061

Original comment by angelo.z...@gmail.com on 10 Jan 2013 at 7:11