File: AbstractSepaParser.java

package info (click to toggle)
hbci4java 4.1.6%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,256 kB
  • sloc: java: 52,465; xml: 34,233; cpp: 3,264; makefile: 153; sh: 11
file content (85 lines) | stat: -rw-r--r-- 2,644 bytes parent folder | download | duplicates (2)
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
package org.kapott.hbci.GV.parsers;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.Properties;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.util.StreamReaderDelegate;

import org.kapott.hbci.manager.HBCIUtils;
import org.kapott.hbci.sepa.SepaVersion;
import org.kapott.hbci.tools.IOUtils;

import javax.xml.bind.JAXB;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;




/**
 * Abstrakte Basis-Klasse der SEPA PAIN-Parser.
 * @param <T> der konkrete Typ.
 */
public abstract class AbstractSepaParser<T> implements ISEPAParser<T>
{
    /**
     * Speichert den Wert in den Properties.
     * @param props die Properties.
     * @param name das Property.
     * @param value der Wert.
     */
    void put(Properties props, Names name, String value)
    {
        // BUGZILLA 1610 - "java.util.Properties" ist von Hashtable abgeleitet und unterstuetzt keine NULL-Werte
        if (value == null)
            return;
        
        props.setProperty(name.getValue(),value);
    }
    
    /**
     * Parst das XML fehlertolerant auch dann, wenn kein Namespace im XML angegeben ist.
     * @param <R> der Typ des Root-Elements.
     * @param is der InputStream.
     * @param version die erwartete SEPA-Version.
     * @param root der Typ des Root-Elements.
     * @return das Root-Element.
     */
    protected <R> R parse(InputStream is, SepaVersion version, Class<R> root)
    {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try
      {
        IOUtils.copy(is,bos);
        final XMLInputFactory xif = XMLInputFactory.newFactory();
        final XMLStreamReader xsr = new StreamReaderDelegate(xif.createXMLStreamReader(new ByteArrayInputStream(bos.toByteArray()))) {
          /**
           * @see javax.xml.stream.util.StreamReaderDelegate#getNamespaceURI()
           */
          @Override
          public String getNamespaceURI()
          {
            String uri = super.getNamespaceURI();
            if (uri == null || uri.isEmpty())
              return version.getURN();
            return uri;
          }
        };
        
        final JAXBContext ctx = JAXBContext.newInstance(root);
        final Unmarshaller u = ctx.createUnmarshaller();
        final JAXBElement jaxb = (JAXBElement) u.unmarshal(xsr);
        return (R) jaxb.getValue();
      }
      catch (Exception e)
      {
        HBCIUtils.log(e);
        return JAXB.unmarshal(new ByteArrayInputStream(bos.toByteArray()),root);
      }
    }
}