-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathDefaultEbicsRootElement.java
More file actions
212 lines (187 loc) · 6.34 KB
/
DefaultEbicsRootElement.java
File metadata and controls
212 lines (187 loc) · 6.34 KB
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
/*
* Copyright (c) 1990-2012 kopiLeft Development SARL, Bizerte, Tunisia
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
package org.kopi.ebics.xml;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import org.apache.xml.security.c14n.Canonicalizer;
import org.apache.xmlbeans.XmlCursor;
import org.apache.xmlbeans.XmlError;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.kopi.ebics.exception.EbicsException;
import org.kopi.ebics.interfaces.EbicsOrderType;
import org.kopi.ebics.interfaces.EbicsRootElement;
import org.kopi.ebics.session.EbicsSession;
import org.kopi.ebics.utils.Utils;
public abstract class DefaultEbicsRootElement implements EbicsRootElement {
/**
* Constructs a new default <code>EbicsRootElement</code>
* @param session the current ebics session
*/
protected DefaultEbicsRootElement(EbicsSession session) {
this.session = session;
suggestedPrefixes = new HashMap<>();
}
/**
* Constructs a new default <code>EbicsRootElement</code>
*/
protected DefaultEbicsRootElement() {
this(null);
}
/**
* Saves the Suggested Prefixes when the XML is printed
* @param uri the namespace URI
* @param prefix the namespace URI prefix
*/
protected void setSaveSuggestedPrefixes(String uri, String prefix) {
suggestedPrefixes.put(uri, prefix);
}
/**
* Prints a pretty XML document in Canonical XML.
* @return the canonical XML document.
*/
public byte[] prettyPrint() {
try {
Canonicalizer canon = Canonicalizer.getInstance(Canonicalizer.ALGO_ID_C14N_OMIT_COMMENTS);
var bos = new ByteArrayOutputStream();
canon.canonicalize(toByteArray(), bos, true);
return bos.toByteArray();
} catch (Exception e) {
throw new RuntimeException("Failed to canonicalize XML", e);
}
}
public String toPrettyString() {
return new String(prettyPrint(), StandardCharsets.UTF_8);
}
/**
* Inserts a schema location to the current ebics root element.
* @param namespaceURI the name space URI
* @param localPart the local part
* @param prefix the prefix
* @param value the value
*/
public void insertSchemaLocation(String namespaceURI,
String localPart,
String prefix,
String value)
{
try (XmlCursor cursor = document.newCursor()) {
while (cursor.hasNextToken()) {
if (cursor.isStart()) {
cursor.toNextToken();
cursor.insertAttributeWithValue(new QName(namespaceURI, localPart, prefix),
value);
break;
} else {
cursor.toNextToken();
}
}
}
}
/**
* Generates a random file name with a prefix.
* @param type the order type.
* @return the generated file name.
*/
public static String generateName(EbicsOrderType type) {
return type.getCode() + new BigInteger(130, Utils.secureRandom).toString(32);
}
/**
* Generates a random file name with a prefix.
* @param prefix the prefix to use.
* @return the generated file name.
*/
public static String generateName(String prefix) {
return prefix + new BigInteger(130, Utils.secureRandom).toString(32);
}
@Override
public String toString() {
return new String(toByteArray());
}
@Override
public byte[] toByteArray() {
XmlOptions options;
options = new XmlOptions();
options.setSavePrettyPrint();
options.setSaveSuggestedPrefixes(suggestedPrefixes);
return document.xmlText(options).getBytes();
}
@Override
public void addNamespaceDecl(String prefix, String uri) {
try (XmlCursor cursor = document.newCursor()) {
while (cursor.hasNextToken()) {
if (cursor.isStart()) {
cursor.toNextToken();
cursor.insertNamespace(prefix, uri);
break;
} else {
cursor.toNextToken();
}
}
}
}
@Override
public void validate() throws EbicsException {
List<XmlError> validationMessages = new ArrayList<>();
boolean isValid = document.validate(new XmlOptions().setErrorListener(validationMessages));
if (!isValid) {
Iterator<XmlError> iter = validationMessages.iterator();
StringBuilder message = new StringBuilder();
while (iter.hasNext()) {
if (!message.toString().isEmpty()) {
message.append(";");
}
message.append(iter.next().getMessage());
}
throw new EbicsException(
"Invalid " + this.getClass().getSimpleName() + ": " + message);
}
}
@Override
public void save(OutputStream out) throws EbicsException {
try {
byte[] element = prettyPrint();
out.write(element);
out.flush();
out.close();
} catch (IOException e) {
throw new EbicsException(e.getMessage());
}
}
@Override
public void print(PrintStream stream) {
stream.println(document.toString());
}
// --------------------------------------------------------------------
// DATA MEMBERS
// --------------------------------------------------------------------
protected XmlObject document;
protected EbicsSession session;
private final Map<String, String> suggestedPrefixes;
private static final long serialVersionUID = -3928957097145095177L;
}