Search in sources :

Example 51 with MatchResult

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;
}
Also used : ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) MatcherResult(com.github.anba.es6draft.regexp.MatcherResult) MatcherState(com.github.anba.es6draft.regexp.MatcherState) MatchResult(java.util.regex.MatchResult)

Example 52 with MatchResult

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();
}
Also used : ScriptObject(com.github.anba.es6draft.runtime.types.ScriptObject) OrdinaryObject(com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject) ArrayObject(com.github.anba.es6draft.runtime.types.builtins.ArrayObject) MatchResult(java.util.regex.MatchResult)

Example 53 with MatchResult

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);
    }
}
Also used : Scanner(java.util.Scanner) Base64(org.apache.commons.codec.binary.Base64) Message(org.apache.cxf.message.Message) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Fault(org.apache.cxf.interceptor.Fault) MatchResult(java.util.regex.MatchResult) IOException(java.io.IOException) XMLStreamException(javax.xml.stream.XMLStreamException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 54 with MatchResult

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);
    }
}
Also used : Scanner(java.util.Scanner) XMLStreamException(javax.xml.stream.XMLStreamException) InputStream(java.io.InputStream) Fault(org.apache.cxf.interceptor.Fault) MatchResult(java.util.regex.MatchResult) XMLStreamException(javax.xml.stream.XMLStreamException) CachedOutputStream(org.apache.cxf.io.CachedOutputStream)

Example 55 with MatchResult

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();
}
Also used : Matcher(java.util.regex.Matcher) MatchResult(java.util.regex.MatchResult)

Aggregations

MatchResult (java.util.regex.MatchResult)62 Matcher (java.util.regex.Matcher)26 Pattern (java.util.regex.Pattern)16 Scanner (java.util.Scanner)11 Point (java.awt.Point)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 MatcherState (com.github.anba.es6draft.regexp.MatcherState)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 InputStream (java.io.InputStream)2 Fault (org.apache.cxf.interceptor.Fault)2 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)2 IterableMatchResult (com.github.anba.es6draft.regexp.IterableMatchResult)1 MatcherResult (com.github.anba.es6draft.regexp.MatcherResult)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1