Bonjour

Je cherche a generer un PDF depuis une servlet avec des Donnees XML et non pas des données base de donnee
J'ai deux exemples qui fonctionne le PDFServlet de la demo Jasper et un exemple en Main qui me donne des données a partir du XML et du JRXML .
Mais pour la sortie servlet mon document est vide alors que le tableau de bytes est bien renseignée .
Ou est mon erreur Merci
Philippe
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
 
/*
 * pour l'instant cree un fichier PDF vide en parant du .jrxml
 */
 
package jasperxml01;
 
// http://82.247.148.50/jasper204/servlet/jasperxml01.Demo4
 
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
 
import java.io.IOException;
 
import java.io.PrintWriter;
 
import net.sf.jasperreports.engine.*;
import java.util.HashMap;
 
import java.util.Map;
 
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
 
import javax.servlet.http.HttpServletRequest;
 
import javax.servlet.http.HttpServletResponse;
 
public class Demo4 extends HttpServlet
{
    /**
     *
     */
    public void service(
            HttpServletRequest request,
            HttpServletResponse response
            ) throws IOException, ServletException
    {
            ServletContext context = this.getServletConfig().getServletContext();
 
 
 
        String xmlFileName = context.getRealPath("/reports/addressbook.xml"); // fichier de donnee Initiale
         String jrxmlFileName = context.getRealPath("/reports/Addressbook.jrxml"); // fichier de conception initiale
 
         //Uniquement pour les parametres
          File reportFile = new File(context.getRealPath("/reports/Addressbook.jrxml"));
          if (!reportFile.exists())
                  throw new JRRuntimeException("File WebappReport.jasper not found. The report design must be compiled first.");
        String recordPath = "/addressbook/category/person";
 
    System.out.println("demo4 "+xmlFileName);
                System.out.println("demo4 "+jrxmlFileName);
        // recuperation des Donées XML            
         JRXMLDataSource jrxmlds = new JRXMLDataSource(xmlFileName,recordPath);
 
 
                    HashMap hm = new HashMap();
                hm.put("ReportTitle", "Address Report");
                hm.put("BaseDir", reportFile.getParentFile());
 
 
 
                    byte[] bytes = null;
 
                    try
                    {
                    		    bytes =    JasperRunManager.runReportToPdf(JasperCompileManager.compileReport(jrxmlFileName), hm, jrxmlds);
                        System.out.println("demo4 bytes.length="+bytes.length);
 
                    }
                    catch (JRException e)
                    {
                            response.setContentType("text/html");
                            PrintWriter out = response.getWriter();
                            out.println("<html>");
                            out.println("<head>");
                            out.println("<title>JasperReports - Web Application Sample</title>");
                            out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
                            out.println("</head>");
 
                            out.println("<body bgcolor=\"white\">");
 
                            out.println("<span class=\"bnew\">JasperReports encountered this error :</span>");
                            out.println("<pre>");
 
                            e.printStackTrace(out);
 
                            out.println("</pre>");
 
                            out.println("</body>");
                            out.println("</html>");
 
                            return;
                    }
 
                    if (bytes != null && bytes.length > 0)
                    {
                            response.setContentType("application/pdf");
                            response.setContentLength(bytes.length);
                            ServletOutputStream ouputStream = response.getOutputStream();
                            ouputStream.write(bytes, 0, bytes.length);
                            ouputStream.flush();
                            ouputStream.close();
                    }
                    else
                    {
                            response.setContentType("text/html");
                            PrintWriter out = response.getWriter();
                            out.println("<html>");
                            out.println("<head>");
                            out.println("<title>JasperReports - Web Application Sample</title>");
                            out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"../stylesheet.css\" title=\"Style\">");
                            out.println("</head>");
 
                            out.println("<body bgcolor=\"white\">");
 
                            out.println("<span class=\"bold\">Empty response.</span>");
 
                            out.println("</body>");
                            out.println("</html>");
                    }
 
            }
 
 
}
 
package jasperxml01;
 
 
public class FieldNode { 
 
 
 
 
 
    private java.util.Properties attributes; 
    private java.util.Vector children; 
    private String name = ""; 
    private String value = null; 
    private int childIndex = 0; 
    //private booleanboolean consumed = false; 
    private boolean consumed = false; 
 
    public FieldNode(String name) { 
        super(); 
        attributes = new java.util.Properties(); 
        children = new java.util.Vector(); 
        this.name = name; 
    } 
 
    public java.util.Properties getAttributes() { 
        return attributes; 
    } 
 
    public void setAttributes(java.util.Properties attributes) { 
        this.attributes = attributes; 
    } 
 
    public java.lang.String getName() { 
        return name; 
    } 
 
    public void setName(java.lang.String name) { 
        this.name = name; 
    } 
 
    public java.lang.String getValue() { 
        return value; 
    } 
 
    public java.lang.String getValue(String def) { 
        if (value == null || value.length() == 0) return def; 
        return value; 
    } 
 
  //  public booleanboolean getValueAsBoolean() {    
    public boolean getValueAsBoolean() { 
        if (value != null && value.trim().equalsIgnoreCase("true")) return true; 
        return false; 
    } 
 
    public int getValueAsInteger() { 
        try { 
            return Integer.parseInt(value + ""); 
        } catch (Exception ex) { 
            return 0; 
        } 
    } 
 
    public void setValue(java.lang.String value) { 
        this.value = value; 
    } 
 
    //public FieldNode getChild(java.lang.String childName, booleanboolean noNull) {      
    public FieldNode getChild(java.lang.String childName, boolean noNull) { 
        java.util.Enumeration enum_children = children.elements(); 
        while (enum_children.hasMoreElements()) { 
            FieldNode cn = (FieldNode)enum_children.nextElement(); 
            if (cn.getName().equals(childName)) return cn; 
        } 
        if (noNull) { 
            FieldNode cn = new FieldNode(childName); 
            this.children.add(cn); 
            return cn; 
        } 
        return null; 
    } 
 
    public java.util.Vector getChilddren(java.lang.String childName) { 
        java.util.Vector result = new java.util.Vector(); 
        java.util.Enumeration enum_children = children.elements(); 
        while (enum_children.hasMoreElements()) { 
            FieldNode cn = (FieldNode)enum_children.nextElement(); 
            if (cn.getName().equals(childName)) { 
                result.add(cn); 
            } 
        } 
        return result; 
    } 
 
    public FieldNode getChild(java.lang.String childName) { 
        return getChild(childName, true); 
    } 
 
    public FieldNode getNextChild() { 
        if (this.getChildren().size() == 0) { 
            return new FieldNode("Unnamed"); 
        } 
        if (childIndex < 0) { 
            childIndex = 0; 
        } 
        return (FieldNode)this.getChildren().elementAt(childIndex); 
    } 
 
    public java.lang.String getAttribute(String attrName) { 
        return attributes.getProperty(attrName); 
    } 
 
    public java.lang.String getAttribute(String attrName, String def) { 
        return attributes.getProperty(attrName, def); 
    } 
 
   // public booleanboolean getAttributeAsBoolean(String attrName) {  
    public boolean getAttributeAsBoolean(String attrName) { 
        String val = attributes.getProperty(attrName); 
        if (val != null && val.trim().equalsIgnoreCase("true")) return true; 
        return false; 
    } 
 
    public int getAttributeAsInteger(String attrName) { 
        try { 
            String val = attributes.getProperty(attrName); 
            return Integer.parseInt(val + ""); 
        } catch (Exception ex) { 
            return 0; 
        } 
    } 
 
    public java.util.Vector getChildren() { 
        return children; 
    } 
 
    public void setChildren(java.util.Vector children) { 
        this.children = children; 
    } 
 
   // public booleanboolean next(String xmlPath) {  
    public boolean next(String xmlPath) { 
        if (xmlPath.startsWith("/")) { 
            xmlPath = xmlPath.substring(1); 
        } 
        if (xmlPath.indexOf("/") < 0) { 
            if (!consumed) { 
                return consumed = true; 
            } else  return false; 
        } 
        xmlPath = xmlPath.substring(xmlPath.indexOf("/")); 
        if (childIndex == 0 && getChildren().size() == 0) { 
            if (!consumed) { 
                return consumed = true; 
            } else  return false; 
        } 
        if (!getNextChild().next(xmlPath)) { 
            childIndex++; 
            if (childIndex < getChildren().size()) return getNextChild().next(xmlPath); 
        } else  { 
            return true; 
        } 
        return false; 
    } 
 
    public String getChildsPath(String xmlPath) { 
        if (xmlPath.indexOf("/") == xmlPath.lastIndexOf("/")) { 
            return ""; 
        } 
        xmlPath = xmlPath.substring(xmlPath.indexOf("/") + 1); 
        return this.getNextChild().getName() + "(" + childIndex + ")::" + this.getNextChild().getChildsPath(xmlPath); 
    } 
}
 
 
 
public class JRXMLDataSource implements net.sf.jasperreports.engine.JRDataSource { 
 
 
 
 
 
    private FieldNode rootFieldNode = null; 
    private String rowPath = "/"; 
    private FieldNode actualPath = null; 
 
    public JRXMLDataSource(String uri, String rowPath) { 
        super(); 
        this.rowPath = rowPath; 
        try { 
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
            org.w3c.dom.Document doc = db.parse(uri); 
            build(doc); 
        } catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
    } 
 
    public JRXMLDataSource(java.io.File file, String rowPath) { 
        super(); 
        this.rowPath = rowPath; 
        try { 
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
            org.w3c.dom.Document doc = db.parse(file); 
            build(doc); 
        } catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
    } 
 
    public JRXMLDataSource(java.io.InputStream is, String rowPath) { 
        super(); 
        this.rowPath = rowPath; 
        try { 
            DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
            org.w3c.dom.Document doc = db.parse(is); 
            build(doc); 
        } catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
    } 
 
    public JRXMLDataSource(FieldNode rootFieldNode, String rowPath) { 
        super(); 
        this.rowPath = rowPath; 
        this.rootFieldNode = rootFieldNode; 
    } 
 
    private void build(org.w3c.dom.Document doc) throws Exception { 
        Element element = doc.getDocumentElement(); 
        rootFieldNode = createNode(element, null); 
    } 
 
    private FieldNode createNode(Node element, FieldNode parent) throws Exception { 
        FieldNode cn = new FieldNode(element.getNodeName()); 
        NamedNodeMap attributes = element.getAttributes(); 
        for (int i = 0; i < attributes.getLength(); ++i) { 
            Node node = attributes.item(i); 
            cn.getAttributes().setProperty(node.getNodeName(), node.getNodeValue()); 
        } 
        NodeList nl = element.getChildNodes(); 
        for (int i = 0; i < nl.getLength(); ++i) { 
            Node node = nl.item(i); 
            if (node.getNodeType() == Node.ELEMENT_NODE) { 
                createNode(node, cn); 
            } else  if (node.getNodeType() == Node.CDATA_SECTION_NODE || node.getNodeType() == Node.TEXT_NODE) { 
                cn.setValue(node.getNodeValue()); 
            } 
        } 
        if (parent != null) { 
            parent.getChildren().add(cn); 
        } else  { 
            parent = cn; 
        } 
        return parent; 
    } 
 
    public Object getFieldValue(net.sf.jasperreports.engine.JRField jRField) throws net.sf.jasperreports.engine.JRException { 
        String path = jRField.getDescription(); 
        Object val = getPathValue(rootFieldNode, path); 
        { 
            return val; 
        } 
    } 
 
 //   public booleanboolean next() throws net.sf.jasperreports.engine.JRException { 
        public boolean next() throws net.sf.jasperreports.engine.JRException { 
        return rootFieldNode.next(this.rowPath); 
    } 
 
    public String getActualPath() { 
        String childPath = this.rowPath; 
        childPath = rowPath.substring(rootFieldNode.getName().length() + 1); 
        return rootFieldNode.getName() + "::" + rootFieldNode.getChildsPath(rowPath); 
    } 
 
    public static void main(String[] argv) { 
        JRXMLDataSource ds = new JRXMLDataSource("C:\\\\test_ireport.xml", "/addressbook/address/ciccio"); 
        try { 
            System.out.println("starting"); 
            while (ds.next()) { 
                System.out.println(ds.getActualPath()); 
            } 
            System.out.println("finishing"); 
        } catch (Exception ex) { 
            ex.printStackTrace(); 
        } 
    } 
 
    private Object getPathValue(FieldNode startingNode, String path) { 
        String tag = ""; 
        if (path == null) return startingNode.getValue(); 
        if (path.startsWith("/")) path = path.substring(1); 
        String sub_path = ""; 
        if (path.indexOf("+") >= 0) { 
            sub_path = path.substring(path.indexOf("+") + 1); 
            path = path.substring(0, path.indexOf("+") + 1); 
        } 
        if (path.indexOf("/") < 0) { 
            if (path.indexOf("@") >= 0) { 
                tag = path.substring(path.indexOf("@") + 1); 
                return startingNode.getAttribute(tag); 
            } else  if (path.indexOf("*") >= 0) { 
                String childName = path.substring(path.indexOf("*") + 1); 
                FieldNode fn = new FieldNode(startingNode.getName()); 
                fn.setAttributes(startingNode.getAttributes()); 
                fn.setChildren(startingNode.getChilddren(childName)); 
                return new JRXMLDataSource(fn, "/" + startingNode.getName() + "/" + childName); 
            } else  if (path.indexOf("+") >= 0) { 
                String childToTake = sub_path; 
                childToTake = getNextNodeName(sub_path); 
                return getSubPathValue(startingNode.getChild(childToTake), sub_path); 
            } else  { 
                return startingNode.getValue(); 
            } 
        } 
        path = path.substring(path.indexOf("/") + 1); 
        path += sub_path; 
        return getPathValue(startingNode.getNextChild(), path); 
    } 
 
    private Object getSubPathValue(FieldNode startingNode, String path) { 
        String tag = ""; 
        if (path == null) return startingNode.getValue() + "[" + path + "]"; 
        if (path.startsWith("/")) path = path.substring(1); 
        if (path.indexOf("/") < 0) { 
            if (path.indexOf("@") >= 0) { 
                tag = path.substring(path.indexOf("@") + 1); 
                return startingNode.getAttribute(tag); 
            } else  if (path.indexOf("*") >= 0) { 
                String childName = path.substring(path.indexOf("*") + 1); 
                FieldNode fn = new FieldNode(startingNode.getName()); 
                fn.setAttributes(startingNode.getAttributes()); 
                fn.setChildren(startingNode.getChilddren(childName)); 
                return new JRXMLDataSource(fn, "/" + startingNode.getName() + "/" + childName); 
            } else  { 
                return startingNode.getValue(); 
            } 
        } else  { 
            path = path.substring(path.indexOf("/") + 1); 
            String childToTake = path; 
            childToTake = getNextNodeName(path); 
            return getSubPathValue(startingNode.getChild(childToTake), path); 
        } 
    } 
 
    private static String getNextNodeName(String path) { 
        if (path == null || path.length() == 0) return ""; 
        if (path.startsWith("/")) path = path.substring(1); 
        String childToTake = path; 
        if (path.indexOf("/") >= 0) { 
            childToTake = path.substring(0, path.indexOf("/")); 
        } 
        if (childToTake.indexOf("@") >= 0) { 
            childToTake = childToTake.substring(0, path.indexOf("@")); 
        } 
        if (childToTake.indexOf("*") >= 0) { 
            childToTake = childToTake.substring(0, path.indexOf("*")); 
        } 
        return childToTake; 
    } 
}