Search in sources :

Example 21 with TextNode

use of com.fasterxml.jackson.databind.node.TextNode in project XRTB by benmfaul.

the class Node method testInternal.

/**
	 * Internal version of test() when recursion is required (NOT_* form)
	 * 
	 * @param value.
	 *            Object. Converts the value of the bid request field (Jackson)
	 *            to the appropriate Java object.
	 * @return boolean. Returns true if the operation succeeded.
	 * @throws Exception
	 *             if the value is not recognized or is not compatible with
	 *             this.value.
	 */
public boolean testInternal(Object value) throws Exception {
    if (value == null || value instanceof MissingNode == true) {
        // request.
        if (!(operator == EXISTS || operator == NOT_EXISTS)) {
            if (notPresentOk)
                return true;
            return false;
        }
    }
    Number nvalue = null;
    String svalue = null;
    //	Set qvalue = null;
    Set qval = null;
    if (value instanceof String)
        svalue = (String) value;
    if (value instanceof IntNode) {
        IntNode n = (IntNode) value;
        nvalue = n.numberValue();
    } else if (value instanceof TextNode) {
        TextNode tn = (TextNode) value;
        svalue = tn.textValue();
    } else if (value instanceof ArrayNode) {
        List list = traverse((ArrayNode) value);
        qvalue = new TreeSet(list);
    } else if (value instanceof ObjectNode) {
        ObjectNode n = (ObjectNode) value;
        mvalue = iterateObject(n);
    } else if (value instanceof Double) {
        // (Node) value;
        DoubleNode n = new DoubleNode((Double) value);
        nvalue = n.numberValue();
    } else if (value instanceof DoubleNode) {
        DoubleNode n = (DoubleNode) value;
        n.asDouble();
        // (Node) value;
        DoubleNode nn = new DoubleNode(n.asDouble());
        nvalue = nn.numberValue();
    } else if (value instanceof Integer) {
        // (Node) value;
        IntNode n = new IntNode((Integer) value);
        nvalue = n.numberValue();
    } else if (value instanceof Collection) {
        qvalue = new TreeSet();
        qvalue.addAll((Collection) value);
    }
    switch(operator) {
        case QUERY:
            return true;
        case EQUALS:
            return processEquals(ival, nvalue, sval, svalue, qval, qvalue);
        case NOT_EQUALS:
            return !processEquals(ival, nvalue, sval, svalue, qval, qvalue);
        case STRINGIN:
            if (lval != null) {
                boolean member = false;
                for (int i = 0; i < lval.size(); i++) {
                    Object test = lval.get(i);
                    if (test instanceof String) {
                        String testS = (String) test;
                        member |= processStringin(ival, nvalue, testS, svalue, qval, qvalue);
                    }
                }
                return member;
            }
            return processStringin(ival, nvalue, sval, svalue, qval, qvalue);
        case NOT_STRINGIN:
            if (lval != null) {
                boolean member = false;
                for (int i = 0; i < lval.size(); i++) {
                    Object test = lval.get(i);
                    if (test instanceof String) {
                        String testS = (String) test;
                        member |= processStringin(ival, nvalue, testS, svalue, qval, qvalue);
                    }
                }
                return !member;
            }
            return !processStringin(ival, nvalue, sval, svalue, qval, qvalue);
        case REGEX:
        case NOT_REGEX:
            boolean member = true;
            if (svalue != null) {
                member = processRegex(ival, nvalue, sval, svalue, qval, qvalue);
            }
            if (operator == NOT_REGEX)
                return !member;
            else
                return member;
        case MEMBER:
        case NOT_MEMBER:
            if (sval != null && (sval.startsWith("@") || sval.startsWith("$"))) {
                Object x = LookingGlass.get(sval);
                boolean t = false;
                if (x instanceof NavMap) {
                    NavMap nm = (NavMap) x;
                    t = nm.contains(svalue);
                } else if (x instanceof BloomFilter) {
                    BloomFilter b = (BloomFilter) x;
                    t = b.mightContain(svalue);
                } else if (x instanceof Set) {
                    Set set = (Set) x;
                    t = set.contains(svalue);
                }
                if (operator == NOT_MEMBER)
                    return !t;
                else
                    return t;
            }
            if (qvalue == null) {
                if (lval != null)
                    qvalue = new HashSet(lval);
                else {
                    qvalue = new HashSet();
                    if (svalue == null)
                        qvalue.addAll((Collection) value);
                    else
                        qvalue.add(svalue);
                }
            }
            if (nvalue == null && svalue == null) {
                if (this.value instanceof String)
                    svalue = (String) this.value;
                else {
                    try {
                        nvalue = (Integer) this.value;
                    } catch (Exception error) {
                        return false;
                    //System.out.println("QVALUE: " + qvalue);
                    //System.out.println("THIS VALUE: " + this.value);
                    //System.out.println("VALUE: " + value);
                    }
                }
            }
            boolean test = false;
            test = processMember(nvalue, svalue, qvalue);
            if (operator == MEMBER)
                return test;
            else
                return !test;
        case INTERSECTS:
        case NOT_INTERSECTS:
            if (qvalue == null) {
                if (lval != null)
                    qvalue = new TreeSet(lval);
            }
            if (qval == null) {
                if (svalue != null) {
                    qval = new TreeSet();
                    qval.add(svalue);
                } else {
                    if (nvalue != null) {
                        qval = new TreeSet();
                        qval.add(nvalue);
                    } else if (lval != null) {
                        qval = new TreeSet(lval);
                    }
                }
            } else {
                if (svalue != null) {
                    qval.add(svalue);
                } else {
                    if (nvalue != null) {
                        qval.add(nvalue.intValue());
                    }
                }
            }
            if (qval == null)
                qval = new TreeSet(lval);
            boolean xxx = processIntersects(qval, qvalue);
            if (operator == INTERSECTS)
                return xxx;
            return !xxx;
        case INRANGE:
            return computeInRange(mvalue, lval);
        case NOT_INRANGE:
            return !computeInRange(mvalue, lval);
        case DOMAIN:
            return computeInDomain(nvalue, lval);
        case NOT_DOMAIN:
            return !computeInDomain(nvalue, lval);
        case LESS_THAN:
        case LESS_THAN_EQUALS:
        case GREATER_THAN:
        case GREATER_THAN_EQUALS:
            return processLTE(operator, ival, nvalue, sval, svalue, qval, qvalue);
        case EXISTS:
        case NOT_EXISTS:
            boolean rc = false;
            if (value == null)
                rc = false;
            else
                rc = value instanceof ObjectNode;
            if (operator == EXISTS)
                return rc;
            return !rc;
        default:
            return false;
    }
}
Also used : TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) MissingNode(com.fasterxml.jackson.databind.node.MissingNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) BloomFilter(com.google.common.hash.BloomFilter) IntNode(com.fasterxml.jackson.databind.node.IntNode) TreeSet(java.util.TreeSet) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) NavMap(com.xrtb.blocks.NavMap) HashSet(java.util.HashSet)

Example 22 with TextNode

use of com.fasterxml.jackson.databind.node.TextNode in project XRTB by benmfaul.

the class C1X method parseSpecial.

/**
         * Process special C1X stuff, sets the exchange name. Sets encoding.
         */
@Override
public boolean parseSpecial() {
    setExchange("c1x");
    usesEncodedAdm = false;
    // C1X uses ISO2 country codes, we can't digest that with our campaign processor, so we
    // will convert it for them and patch the bid request.
    // Use a cache of country codes to keep from creating a lot of objects to be later garbage collected.
    Object o = this.database.get("device.geo.country");
    if (o instanceof MissingNode) {
        return true;
    }
    TextNode country = (TextNode) o;
    TextNode test = null;
    if (country != null) {
        test = cache.get(country.asText());
        if (test == null) {
            String iso3 = isoMap.query(country.asText());
            test = new TextNode(iso3);
        }
        if (test != country)
            database.put("device.geo.country", test);
    }
    return true;
}
Also used : MissingNode(com.fasterxml.jackson.databind.node.MissingNode) TextNode(com.fasterxml.jackson.databind.node.TextNode)

Example 23 with TextNode

use of com.fasterxml.jackson.databind.node.TextNode in project XRTB by benmfaul.

the class Node method iterateObject.

/**
	 * Iterate over a Jackson object and create a Java Map.
	 * 
	 * @param node.
	 *            ObjectNode. The Jackson node to set up as a Map.
	 * @return Map. Returns the Map implementation of the Jackson node.
	 */
Map iterateObject(ObjectNode node) {
    Map m = new HashMap();
    Iterator it = node.iterator();
    it = node.fieldNames();
    while (it.hasNext()) {
        String key = (String) it.next();
        Object s = node.get(key);
        if (s instanceof TextNode) {
            TextNode t = (TextNode) s;
            m.put(key, t.textValue());
        } else if (s instanceof DoubleNode) {
            DoubleNode t = (DoubleNode) s;
            m.put(key, t.numberValue());
        } else if (s instanceof IntNode) {
            IntNode t = (IntNode) s;
            m.put(key, t.numberValue());
        } else
            // indeterminate, need to traverse
            m.put(key, s);
    }
    return m;
}
Also used : IntNode(com.fasterxml.jackson.databind.node.IntNode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Iterator(java.util.Iterator) DoubleNode(com.fasterxml.jackson.databind.node.DoubleNode) TextNode(com.fasterxml.jackson.databind.node.TextNode) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NavMap(com.xrtb.blocks.NavMap)

Aggregations

TextNode (com.fasterxml.jackson.databind.node.TextNode)23 Test (org.junit.Test)8 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)7 IntNode (com.fasterxml.jackson.databind.node.IntNode)7 ArrayList (java.util.ArrayList)7 DoubleNode (com.fasterxml.jackson.databind.node.DoubleNode)6 JCodeModel (com.sun.codemodel.JCodeModel)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 MissingNode (com.fasterxml.jackson.databind.node.MissingNode)4 JType (com.sun.codemodel.JType)4 List (java.util.List)4 TreeNode (com.fasterxml.jackson.core.TreeNode)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 JDefinedClass (com.sun.codemodel.JDefinedClass)2 JDocComment (com.sun.codemodel.JDocComment)2 NavMap (com.xrtb.blocks.NavMap)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Map (java.util.Map)2