forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNamespaceRemovingInputStream.java
More file actions
169 lines (144 loc) · 5.15 KB
/
NamespaceRemovingInputStream.java
File metadata and controls
169 lines (144 loc) · 5.15 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
/*
* Copyright 2010-2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.util;
import java.io.BufferedInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* FilterInputStream implementation that wraps an InputStream containing an XML
* document, and removes the XML namespace attribute from the XML document.
*/
class NamespaceRemovingInputStream extends FilterInputStream {
/** look ahead buffer */
private byte[] lookAheadData = new byte[200];
/** Set to true once the namespace has been removed */
private boolean hasRemovedNamespace = false;
/**
* Constructs a new NamespaceRemovingInputStream wrapping the specified
* InputStream.
*
* @param in
* The InputStream containing an XML document whose XML namespace
* is to be removed.
*/
public NamespaceRemovingInputStream(InputStream in) {
// Wrap our input stream in a buffered input stream to ensure
// that it support mark/reset
super(new BufferedInputStream(in));
}
/* (non-Javadoc)
* @see java.io.FilterInputStream#read()
*/
@Override
public int read() throws IOException {
int b = in.read();
if (b == 'x' && !hasRemovedNamespace) {
lookAheadData[0] = (byte)b;
in.mark(lookAheadData.length);
int bytesRead = in.read(lookAheadData, 1, lookAheadData.length - 1);
in.reset();
String string = new String(lookAheadData, 0, bytesRead + 1);
int numberCharsMatched = matchXmlNamespaceAttribute(string);
if (numberCharsMatched > 0) {
for (int i = 0; i < numberCharsMatched - 1; i++) {
in.read();
}
b = in.read();
hasRemovedNamespace = true;
}
}
return b;
}
/* (non-Javadoc)
* @see java.io.FilterInputStream#read(byte[], int, int)
*/
@Override
public int read(byte[] b, int off, int len) throws IOException {
for (int i = 0; i < len; i++) {
int j = this.read();
if (j == -1) {
if (i == 0) return -1;
return i;
}
b[i + off] = (byte)j;
}
return len;
}
/* (non-Javadoc)
* @see java.io.FilterInputStream#read(byte[])
*/
@Override
public int read(byte[] b) throws IOException {
return this.read(b, 0, b.length);
}
/**
* Checks if the string starts with a complete XML namespace attribute, and
* if so, returns the number of characters that match.
*
* @param s
* The string to check for an XML namespace definition.
*
* @return -1 if no XML namespace definition was found, otherwise the length
* of the identified XML namespace definition.
*/
private int matchXmlNamespaceAttribute(String s) {
/*
* The regex we're simulating is: "xmlns\\s*=\\s*\".+?\".*"
*/
StringPrefixSlicer stringSlicer = new StringPrefixSlicer(s);
if (stringSlicer.removePrefix("xmlns") == false) return -1;
stringSlicer.removeRepeatingPrefix(" ");
if (stringSlicer.removePrefix("=") == false) return -1;
stringSlicer.removeRepeatingPrefix(" ");
if (stringSlicer.removePrefix("\"") == false) return -1;
if (stringSlicer.removePrefixEndingWith("\"") == false) return -1;
return s.length() - stringSlicer.getString().length();
}
/**
* Utility class to help test and remove specified prefixes from a string.
*/
private static final class StringPrefixSlicer {
private String s;
public StringPrefixSlicer(String s) {
this.s = s;
}
/**
* @return The remaining String (minus any prefixes that have been
* removed).
*/
public String getString() {
return s;
}
public boolean removePrefix(String prefix) {
if (s.startsWith(prefix) == false) return false;
s = s.substring(prefix.length());
return true;
}
public boolean removeRepeatingPrefix(String prefix) {
if (s.startsWith(prefix) == false) return false;
while (s.startsWith(prefix)) {
s = s.substring(prefix.length());
}
return true;
}
public boolean removePrefixEndingWith(String marker) {
int i = s.indexOf(marker);
if (i < 0) return false;
s = s.substring(i + marker.length());
return true;
}
}
}