LayerSourceConverter.java
7.59 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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.scene7.is.provider.LayerSourceEnum
* com.scene7.is.util.Converter
* com.scene7.is.util.collections.CollectionUtil
* com.scene7.is.util.text.CharSetBuilder
* com.scene7.is.util.text.Parser
* com.scene7.is.util.text.ParserUtil
* com.scene7.is.util.text.ParsingException
* com.scene7.is.util.text.Scanner
* com.scene7.is.util.text.coders.SelectiveURLCoder
* com.scene7.is.util.text.parsers.URLDecodeParser
* org.apache.commons.beanutils.ConversionException
* org.jetbrains.annotations.NotNull
*/
package com.scene7.is.ps.provider.parsers.query;
import com.scene7.is.provider.LayerSourceEnum;
import com.scene7.is.ps.provider.EmbeddedLayerSource;
import com.scene7.is.ps.provider.ImageLayerSource;
import com.scene7.is.ps.provider.LayerSource;
import com.scene7.is.ps.provider.Util;
import com.scene7.is.util.Converter;
import com.scene7.is.util.collections.CollectionUtil;
import com.scene7.is.util.text.CharSetBuilder;
import com.scene7.is.util.text.Parser;
import com.scene7.is.util.text.ParserUtil;
import com.scene7.is.util.text.ParsingException;
import com.scene7.is.util.text.Scanner;
import com.scene7.is.util.text.coders.SelectiveURLCoder;
import com.scene7.is.util.text.parsers.URLDecodeParser;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.BitSet;
import java.util.Map;
import org.apache.commons.beanutils.ConversionException;
import org.jetbrains.annotations.NotNull;
public class LayerSourceConverter
extends Converter<String, LayerSource> {
private static final Map<String, LayerSourceEnum> PREFIX_TO_TYPE = CollectionUtil.lookupTable(LayerSourceEnum.class);
private static final BitSet CLOSING_BRACES = CollectionUtil.bitSet((String)"})");
private static final boolean[] ALPHA = CharSetBuilder.charSetBuilder().setRange(true, 'A', 'Z').setRange(true, 'a', 'z').getProduct();
private static final boolean[] OPENING_BRACES = CharSetBuilder.charSetBuilder().set(true, new char[]{'{', '('}).getProduct();
private static final Parser<LayerSourceEnum> TYPE_PARSER = ParserUtil.enumParser(LayerSourceEnum.class, (boolean)false);
private static final Converter<String, LayerSource> INSTANCE = new LayerSourceConverter();
private static final SelectiveURLCoder PATH_CODER = SelectiveURLCoder.selectiveUrlCoder((String)"/");
@NotNull
public static Converter<String, LayerSource> layerSourceConverter() {
return INSTANCE;
}
@NotNull
private static ImageLayerSource imageLayerSource(@NotNull String decoded) throws ParsingException {
try {
return ImageLayerSource.imageLayerSource(decoded);
}
catch (IllegalArgumentException e) {
String message = "Illegal image path: " + e.getMessage();
throw new ParsingException(4, message, (Throwable)e);
}
}
private static boolean isEmbeddedSource(String value, int code) {
return LayerSourceConverter.contains(OPENING_BRACES, code) && LayerSourceConverter.endsWith(value, LayerSourceConverter.closingBrace(code));
}
private static char openingBrace(char lastChar) {
if (lastChar == '}') {
return '{';
}
if (lastChar == ')') {
return '(';
}
throw new AssertionError(lastChar);
}
private static void collectAll(StringReader in, StringBuilder buffer) {
try {
Scanner.collectAll((Reader)in, (StringBuilder)buffer);
}
catch (IOException e) {
throw new AssertionError(e);
}
}
private static int collectWhile(StringReader in, StringBuilder buffer, boolean[] charSet, boolean collectOutSideRange) {
try {
return Scanner.collectWhile((Reader)in, (StringBuilder)buffer, (boolean[])charSet, (boolean)collectOutSideRange);
}
catch (IOException e) {
throw new AssertionError(e);
}
}
private static boolean contains(boolean[] charset, int code) {
return code != -1 && code <= charset.length && charset[code];
}
private static boolean endsWith(@NotNull CharSequence value, char code) {
return code == value.charAt(value.length() - 1);
}
@NotNull
private static String decode(@NotNull String value) throws ParsingException {
return (String)URLDecodeParser.urlDecodeParser().parse(value);
}
private static char closingBrace(int openingBrace) {
switch (openingBrace) {
case 123: {
return '}';
}
case 40: {
return ')';
}
}
throw new AssertionError(openingBrace);
}
@NotNull
public LayerSource convert(@NotNull String value) throws ConversionException {
try {
if (value.isEmpty()) {
throw new ParsingException(7, value, null);
}
if (Util.isInvalidPath(value)) {
throw new ParsingException(1, value, null);
}
StringReader in = new StringReader(value);
StringBuilder buffer = new StringBuilder();
int code = LayerSourceConverter.collectWhile(in, buffer, ALPHA, false);
if (LayerSourceConverter.isEmbeddedSource(value, code)) {
String embeddedSource = buffer.toString();
buffer.setLength(0);
LayerSourceEnum type = embeddedSource.isEmpty() ? LayerSourceEnum.URL : (LayerSourceEnum)TYPE_PARSER.parse(embeddedSource);
LayerSourceConverter.collectAll(in, buffer);
String embeddedRequest = buffer.substring(1, buffer.length() - 1);
return EmbeddedLayerSource.embeddedLayerSource(type, embeddedRequest);
}
String decoded = LayerSourceConverter.decode(value);
return LayerSourceConverter.decodedLayerSource(decoded);
}
catch (ParsingException e) {
throw new ConversionException((Throwable)e);
}
}
@NotNull
public String revert(@NotNull LayerSource value) throws ConversionException {
if (value instanceof ImageLayerSource) {
ImageLayerSource src = (ImageLayerSource)value;
return PATH_CODER.encode(src.getValue());
}
if (value instanceof EmbeddedLayerSource) {
EmbeddedLayerSource src = (EmbeddedLayerSource)value;
StringBuilder result = new StringBuilder();
if (src.getType() != LayerSourceEnum.URL) {
result.append(src.getType().toString().toLowerCase());
}
result.append('(').append(src.getValue()).append(')');
return result.toString();
}
throw new AssertionError((Object)("Layer Source String Converter - Invalid Layer Source Value: " + value.toString()));
}
@NotNull
private static LayerSource decodedLayerSource(@NotNull String value) throws ParsingException {
String prefix;
char lastChar;
int index;
LayerSourceEnum type;
char openingBrace;
if (value.length() > 2 && CLOSING_BRACES.get(lastChar = value.charAt(value.length() - 1)) && (index = value.indexOf(openingBrace = LayerSourceConverter.openingBrace(lastChar))) != -1 && (type = PREFIX_TO_TYPE.get(prefix = value.substring(0, index).toUpperCase())) != null) {
String request = value.substring(index + 1, value.length() - 1);
return EmbeddedLayerSource.embeddedLayerSource(type, request);
}
return LayerSourceConverter.imageLayerSource(value);
}
private LayerSourceConverter() {
super(String.class, LayerSource.class);
}
}