Example usage for java.util.regex Pattern flags

List of usage examples for java.util.regex Pattern flags

Introduction

In this page you can find the example usage for java.util.regex Pattern flags.

Prototype

int flags

To view the source code for java.util.regex Pattern flags.

Click Source Link

Document

The original pattern flags.

Usage

From source file:Main.java

/**
 * Pattern.pattern and Pattern.toString ignore any flags supplied to
 * Pattern.compile, so the regular expression you get out doesn't
 * correspond to what the Pattern was actually matching. This fixes that.
 * /*from w  w  w.j a  v  a 2s  . c  o m*/
 * Note that there are some flags that can't be represented.
 * 
 * FIXME: why don't we use Pattern.LITERAL instead of home-grown escaping
 * code? Is it because you can't do the reverse transformation? Should we
 * integrate that code with this?
 */
public static String toString(Pattern pattern) {
    String regex = pattern.pattern();
    final int flags = pattern.flags();
    if (flags != 0) {
        StringBuilder builder = new StringBuilder("(?");
        toStringHelper(builder, flags, Pattern.UNIX_LINES, 'd');
        toStringHelper(builder, flags, Pattern.CASE_INSENSITIVE, 'i');
        toStringHelper(builder, flags, Pattern.COMMENTS, 'x');
        toStringHelper(builder, flags, Pattern.MULTILINE, 'm');
        toStringHelper(builder, flags, Pattern.DOTALL, 's');
        toStringHelper(builder, flags, Pattern.UNICODE_CASE, 'u');
        builder.append(")");
        regex = builder.toString() + regex;
    }
    return regex;
}

From source file:com.github.nlloyd.hornofmongo.util.BSONizer.java

@SuppressWarnings("deprecation")
public static Object convertBSONtoJS(MongoScope mongoScope, Object bsonObject) {
    Object jsObject = null;/*from w w  w. ja  v  a 2  s  .  c  o m*/
    if (bsonObject instanceof List<?>) {
        List<?> bsonList = (List<?>) bsonObject;
        Scriptable jsArray = (Scriptable) MongoRuntime.call(new NewInstanceAction(mongoScope, bsonList.size()));

        int index = 0;
        for (Object bsonEntry : bsonList) {
            ScriptableObject.putProperty(jsArray, index, convertBSONtoJS(mongoScope, bsonEntry));
            index++;
        }

        jsObject = jsArray;
    } else if (bsonObject instanceof BSONObject) {
        Scriptable jsObj = (Scriptable) MongoRuntime.call(new NewInstanceAction(mongoScope));
        BSONObject bsonObj = (BSONObject) bsonObject;

        for (String key : bsonObj.keySet()) {
            Object value = convertBSONtoJS(mongoScope, bsonObj.get(key));
            MongoRuntime.call(new JSPopulatePropertyAction(jsObj, key, value));
        }
        jsObject = jsObj;
    } else if (bsonObject instanceof Symbol) {
        jsObject = ((Symbol) bsonObject).getSymbol();
    } else if (bsonObject instanceof Date) {
        jsObject = MongoRuntime.call(
                new NewInstanceAction(mongoScope, "Date", new Object[] { ((Date) bsonObject).getTime() }));
    } else if (bsonObject instanceof Pattern) {
        Pattern regex = (Pattern) bsonObject;
        String source = regex.pattern();
        String options = Bytes.regexFlags(regex.flags());
        jsObject = MongoRuntime
                .call(new NewInstanceAction(mongoScope, "RegExp", new Object[] { source, options }));
    } else if (bsonObject instanceof org.bson.types.ObjectId) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "ObjectId"));
        ((ObjectId) jsObject).setRealObjectId((org.bson.types.ObjectId) bsonObject);
    } else if (bsonObject instanceof org.bson.types.MinKey) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "MinKey"));
    } else if (bsonObject instanceof org.bson.types.MaxKey) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "MaxKey"));
    } else if (bsonObject instanceof com.mongodb.DBRef) {
        com.mongodb.DBRef dbRef = (com.mongodb.DBRef) bsonObject;
        Object id = convertBSONtoJS(mongoScope, dbRef.getId());
        jsObject = MongoRuntime.call(
                new NewInstanceAction(mongoScope, "DBRef", new Object[] { dbRef.getCollectionName(), id }));
    } else if (bsonObject instanceof BSONTimestamp) {
        BSONTimestamp bsonTstamp = (BSONTimestamp) bsonObject;
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "Timestamp",
                new Object[] { bsonTstamp.getTime(), bsonTstamp.getInc() }));
    } else if (bsonObject instanceof Long) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "NumberLong"));
        ((NumberLong) jsObject).setRealLong((Long) bsonObject);
    } else if (bsonObject instanceof Integer) {
        jsObject = Double.valueOf((Integer) bsonObject);
    } else if (bsonObject instanceof Code) {
        jsObject = ((Code) bsonObject).getCode();
    } else if (bsonObject instanceof byte[]) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "BinData"));
        ((BinData) jsObject).setValues(0, (byte[]) bsonObject);
    } else if (bsonObject instanceof Binary) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "BinData"));
        ((BinData) jsObject).setValues(((Binary) bsonObject).getType(), ((Binary) bsonObject).getData());
    } else if (bsonObject instanceof UUID) {
        jsObject = MongoRuntime.call(new NewInstanceAction(mongoScope, "BinData"));
        UUID uuid = (UUID) bsonObject;
        ByteBuffer dataBuffer = ByteBuffer.allocate(16);
        // mongodb wire protocol is little endian
        dataBuffer.order(ByteOrder.LITTLE_ENDIAN);
        dataBuffer.putLong(uuid.getMostSignificantBits());
        dataBuffer.putLong(uuid.getLeastSignificantBits());
        ((BinData) jsObject).setValues(BSON.B_UUID, dataBuffer.array());
    } else {
        jsObject = bsonObject;
    }

    return jsObject;
}

From source file:com.ibm.watson.catalyst.jumpqa.matcher.EntryPatterns.java

@Override
public int hashCode() {
    HashCodeBuilder result = new HashCodeBuilder(SEED, MULTIPLY);
    if (_titlePattern.isPresent()) {
        Pattern p1 = _titlePattern.get();
        result.append(p1.pattern());//from   w w  w. j  ava2  s .co  m
        result.append(p1.flags());
    }
    if (_answerPattern.isPresent()) {
        Pattern p2 = _answerPattern.get();
        result.append(p2.pattern());
        result.append(p2.flags());
    }
    if (_textPattern.isPresent()) {
        Pattern p3 = _textPattern.get();
        result.append(p3.pattern());
        result.append(p3.flags());
    }
    return result.hashCode();
}

From source file:com.ibm.watson.catalyst.jumpqa.matcher.EntryPatterns.java

private boolean patternsEqual(Optional<Pattern> op1, Optional<Pattern> op2) {
    if (op1.isPresent() ^ op2.isPresent())
        return false;
    if (op1.isPresent() & op2.isPresent()) {
        Pattern p1 = op1.get();
        Pattern p2 = op2.get();/*from   w  ww. java2  s . c  o m*/
        if (!Objects.equals(p1.pattern(), p2.pattern()))
            return false;
        if (!Objects.equals(p1.flags(), p2.flags()))
            return false;
    }
    return true;
}

From source file:de.undercouch.bson4jackson.BsonGeneratorTest.java

@Test
public void patterns() throws Exception {
    Pattern pattern = Pattern.compile("a.*a", Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Map<String, Object> data = new LinkedHashMap<String, Object>();
    data.put("pattern", pattern);

    BSONObject obj = generateAndParse(data);

    Pattern result = (Pattern) obj.get("pattern");
    assertNotNull(result);//from  w  ww.j av  a2 s.c  om
    assertEquals(pattern.pattern(), result.pattern());
    assertEquals(pattern.flags(), result.flags());
}

From source file:de.undercouch.bson4jackson.BsonGenerator.java

/**
 * Write a BSON regex/* w w  w  .j  a v  a  2s.  c  o m*/
 *
 * @param pattern The regex to write
 * @throws IOException If an error occurred in the stream while writing
 */
public void writeRegex(Pattern pattern) throws IOException {
    _writeArrayFieldNameIfNeeded();
    _verifyValueWrite("write regex");
    _buffer.putByte(_typeMarker, BsonConstants.TYPE_REGEX);
    _writeCString(pattern.pattern());
    _writeCString(flagsToRegexOptions(pattern.flags()));
    flushBuffer();
}

From source file:de.undercouch.bson4jackson.BsonParserTest.java

@Test
public void parseComplex() throws Exception {
    BSONObject o = new BasicBSONObject();
    o.put("Timestamp", new BSONTimestamp(0xAABB, 0xCCDD));
    o.put("Symbol", new Symbol("Test"));
    o.put("ObjectId", new org.bson.types.ObjectId(Integer.MAX_VALUE, -2, Integer.MIN_VALUE));
    Pattern p = Pattern.compile(".*",
            Pattern.CASE_INSENSITIVE | Pattern.DOTALL | Pattern.MULTILINE | Pattern.UNICODE_CASE);
    o.put("Regex", p);

    Map<?, ?> data = parseBsonObject(o);
    assertEquals(new Timestamp(0xAABB, 0xCCDD), data.get("Timestamp"));
    assertEquals(new de.undercouch.bson4jackson.types.Symbol("Test"), data.get("Symbol"));
    ObjectId oid = (ObjectId) data.get("ObjectId");
    assertEquals(Integer.MAX_VALUE, oid.getTime());
    assertEquals(-2, oid.getMachine());/*from  w  ww. j a va  2s .  c om*/
    assertEquals(Integer.MIN_VALUE, oid.getInc());
    Pattern p2 = (Pattern) data.get("Regex");
    assertEquals(p.flags(), p2.flags());
    assertEquals(p.pattern(), p2.pattern());
}

From source file:org.diorite.impl.command.PluginCommandBuilderImpl.java

@Override
public PluginCommandBuilderImpl pattern(final Pattern pattern) {
    this.checkPattern();
    this.pattern = Pattern.compile(pattern.pattern(), pattern.flags());
    return this;
}

From source file:org.echocat.jomon.runtime.jaxb.PatternAdapter.java

@Override
public String marshal(Pattern v) throws Exception {
    final String patternAsString;
    if (v != null) {
        patternAsString = "/" + v.pattern() + "/" + toFlagsAsString(v.flags());
    } else {//  ww w  .j a  va  2 s .  c  o m
        patternAsString = null;
    }
    return patternAsString;
}

From source file:org.lockss.util.RegexpUtil.java

public static boolean patEquals(java.util.regex.Pattern p1, java.util.regex.Pattern p2) {
    if (p1 == p2) {
        return true;
    }/*www. j  a va 2s .co  m*/
    if (p1 == null || p2 == null) {
        return false;
    }
    return p1.flags() == p2.flags() && p1.pattern().equals(p2.pattern());
}