use of java.util.regex.MatchResult in project es6draft by anba.
the class RegExpPrototype method RegExpSplit.
private static ArrayObject RegExpSplit(ExecutionContext cx, RegExpObject rx, String s, boolean unicodeMatching, int lim, boolean isShared) {
/* steps 1-10, 13 (not applicable) */
/* step 11 */
ArrayObject a = ArrayCreate(cx, 0);
/* step 12 */
int lengthA = 0;
/* step 14 */
int size = s.length();
/* step 15 */
int p = 0;
/* step 16 */
if (lim == 0) {
return a;
}
/* step 17 */
if (size == 0) {
MatchResult result;
if (isShared) {
result = matchResultOrNull(cx, rx, s, 0, true, true);
} else {
result = matchResultOrNull(cx, rx, s, true);
}
if (result != null) {
return a;
}
CreateDataProperty(cx, a, 0, s);
return a;
}
if (!isShared) {
/* step 19.a */
RegExpSetLastIndex(cx, rx, 0);
}
/* step 18 */
int q = p;
/* step 19 */
int lastStart = -1;
MatcherState matcher = rx.getRegExpMatcher().matcher(s);
boolean storeResult = true, invalidateResult = false;
if (cx.getRuntimeContext().isEnabled(CompatibilityOption.LegacyRegExp)) {
if (cx.getRealm() == rx.getRealm()) {
if (!rx.isLegacyFeaturesEnabled()) {
invalidateResult = true;
storeResult = false;
}
} else {
storeResult = false;
}
}
while (q != size) {
/* steps 19.a-c */
boolean match = matcher.find(q);
if (!match) {
break;
}
MatcherResult result = matcher.toMatchResult();
if (storeResult) {
RegExpConstructor.storeLastMatchResult(cx, s, result);
} else if (invalidateResult) {
RegExpConstructor.invalidateLastMatchResult(cx);
invalidateResult = false;
}
/* steps 19.d.i-ii */
int e = result.end();
/* steps 19.d.iii-iv */
if (e == p) {
/* step 19.d.iii */
q = AdvanceStringIndex(s, q, unicodeMatching);
} else {
/* step 19.d.iv */
String t = s.substring(p, lastStart = result.start());
CreateDataProperty(cx, a, lengthA, t);
lengthA += 1;
if (lengthA == lim) {
if (!isShared) {
RegExpSetLastIndex(cx, rx, e);
}
return a;
}
int groupCount = result.groupCount();
for (int i = 1; i <= groupCount; ++i) {
String cap = result.group(i);
CreateDataProperty(cx, a, lengthA, cap != null ? cap : UNDEFINED);
lengthA += 1;
if (lengthA == lim) {
if (!isShared) {
RegExpSetLastIndex(cx, rx, e);
}
return a;
}
}
p = e;
q = p;
}
}
if (lastStart == size) {
return a;
}
/* step 20 */
String t = s.substring(p, size);
/* step 21 */
CreateDataProperty(cx, a, lengthA, t);
/* step 22 */
return a;
}
use of java.util.regex.MatchResult in project es6draft by anba.
the class RegExpPrototype method RegExpSearch.
private static Object RegExpSearch(ExecutionContext cx, RegExpObject rx, String s) {
// Directly throw TypeErrors instead of saving and restoring the "lastIndex" property.
Object previousLastIndex = rx.getLastIndex().getValue();
boolean lastIndexIsZero = SameValue(previousLastIndex, 0);
if (!lastIndexIsZero) {
RegExpThrowIfLastIndexNonWritable(cx, rx);
}
/* steps 1-3 (not applicable) */
/* steps 4-7 */
boolean sticky = rx.isSet(RegExpObject.Flags.Sticky);
boolean global = rx.isSet(RegExpObject.Flags.Global);
MatchResult result = matchResultOrNull(cx, rx, s, 0, sticky, true);
if (lastIndexIsZero && (global || sticky)) {
// Emulate the lastIndex update from RegExpBuiltinExec.
RegExpThrowIfLastIndexNonWritable(cx, rx);
}
/* step 8 */
if (result == null) {
return -1;
}
/* step 9 */
return result.start();
}
use of java.util.regex.MatchResult in project tesb-rt-se by Talend.
the class CompressionInInterceptor method decompressMessage.
public void decompressMessage(Message message) throws Fault {
if (isGET(message)) {
return;
}
final CachedOutputStream cache = new CachedOutputStream();
final CachedOutputStream decompressedSoapMessage = new CachedOutputStream();
try {
LOG.fine("Uncompressing response");
// Original stream with compressed body
InputStream is = message.getContent(InputStream.class);
if (is == null) {
return;
}
// Loading content of original InputStream to cache
IOUtils.copyAndCloseInput(is, cache);
// Loading SOAP body content to separate stream
CachedOutputStream soapBodyContent = new CachedOutputStream();
Scanner scanner = new Scanner(cache.getInputStream());
MatchResult bodyPosition = null;
try {
bodyPosition = CompressionHelper.loadSoapBodyContent(soapBodyContent, scanner, CompressionConstants.COMPRESSED_SOAP_BODY_PATTERN);
} catch (XMLStreamException e) {
throw new Fault("Can not read compressed SOAP Body", LOG, e, e.getMessage());
}
if (bodyPosition == null) {
// compressed SOAP body content is not found
// skipping decompression
message.setContent(InputStream.class, cache.getInputStream());
} else {
// compressed SOAP body content is found
// apply Base64 decoding for encoded soap body content
final byte[] base64DecodedSoapBody = (new Base64()).decode(soapBodyContent.getBytes());
// uncompress soap body
GZIPInputStream decompressedBody = new GZIPInputStream(new ByteArrayInputStream(base64DecodedSoapBody));
// replace original soap body by compressed one
CompressionHelper.replaceBodyInSOAP(cache.getBytes(), bodyPosition, decompressedBody, decompressedSoapMessage, null, null, true);
message.setContent(InputStream.class, decompressedSoapMessage.getInputStream());
}
if (message.getInterceptorChain() != null) {
message.getInterceptorChain().add(new AbstractPhaseInterceptor<Message>(Phase.POST_INVOKE) {
@Override
public void handleMessage(Message message) throws Fault {
closeCacheStreams(cache, decompressedSoapMessage);
}
});
}
} catch (Exception ex) {
closeCacheStreams(cache, decompressedSoapMessage);
throw new Fault("SOAP Body decompression failed", LOG, ex);
}
}
use of java.util.regex.MatchResult in project tesb-rt-se by Talend.
the class MessageScanerInInterceptor method checkMessage.
public void checkMessage(Message message) throws Fault {
if (isGET(message)) {
return;
}
try {
LOG.info("Checking response");
if (pattern == null || pattern.isEmpty()) {
LOG.info("Pattern is empty");
return;
}
// Original stream with compressed body
InputStream is = message.getContent(InputStream.class);
if (is == null) {
return;
}
// Loading content of original InputStream to cache
CachedOutputStream cache = new CachedOutputStream();
IOUtils.copy(is, cache);
is.close();
// Loading SOAP body content to separate stream
CachedOutputStream soapBodyContent = new CachedOutputStream();
Scanner scanner = new Scanner(cache.getInputStream());
MatchResult patternPosition = null;
try {
patternPosition = CompressionHelper.loadSoapBodyContent(soapBodyContent, scanner, pattern);
if (patternPosition == null) {
patternMatched = false;
if (throwPatternNotMatchedException) {
throw new RuntimeException("Message does not match with pattern {" + pattern + "}");
}
} else {
patternMatched = true;
}
} catch (XMLStreamException e) {
throw new Fault("Can not load SOAP Body content for scaner", LOG, e, e.getMessage());
}
} catch (Exception ex) {
throw new Fault("SOAP Body decompresion failed", LOG, ex);
}
}
use of java.util.regex.MatchResult in project wildfly-swarm by wildfly-swarm.
the class UsageCreator method replaceVariables.
public String replaceVariables(String raw) throws Exception {
if (raw == null) {
return null;
}
Matcher matcher = PATTERN.matcher(raw);
StringBuilder replaced = new StringBuilder();
int cur = 0;
while (matcher.find()) {
MatchResult result = matcher.toMatchResult();
replaced.append(raw.substring(cur, result.start(1)));
String name = result.group(2);
Object value = this.supplier.valueOf(name);
if (value == null) {
value = "${" + name + "}";
}
replaced.append(value);
cur = result.end();
}
replaced.append(raw.substring(cur));
return replaced.toString();
}
Aggregations