Search in sources :

Example 1 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray in project convertigo by convertigo.

the class ElementStep method createStepNodeValue.

@Override
protected void createStepNodeValue(Document doc, Element stepNode) throws EngineException {
    String nodeValue = nodeText;
    if (evaluated != null) {
        nodeValue = "";
        if (evaluated instanceof NodeList) {
            NodeList list = (NodeList) evaluated;
            nodeValue = list.toString();
        } else if (evaluated instanceof Collection) {
            List<String> list = GenericUtils.toString((Collection<?>) evaluated);
            nodeValue = list.toString();
        } else if (evaluated instanceof NativeJavaArray) {
            Object object = ((NativeJavaArray) evaluated).unwrap();
            List<String> list = GenericUtils.toString(Arrays.asList((Object[]) object));
            nodeValue = list.toString();
        } else if (evaluated instanceof NativeArray) {
            nodeValue = (String) ((NativeArray) evaluated).getDefaultValue(String.class);
        } else if (evaluated instanceof NativeJavaObject) {
            nodeValue = (String) ((NativeJavaObject) evaluated).getDefaultValue(String.class);
        } else if (evaluated.getClass().isArray()) {
            nodeValue = Arrays.toString((Object[]) evaluated);
        } else
            nodeValue = evaluated.toString();
    }
    Node text = doc.createTextNode(nodeValue);
    stepNode.appendChild(text);
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Collection(java.util.Collection) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) NodeList(org.w3c.dom.NodeList) List(java.util.List) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) NativeJavaObject(org.mozilla.javascript.NativeJavaObject)

Example 2 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray in project convertigo by convertigo.

the class AttributeStep method createStepNode.

@Override
protected Node createStepNode() throws EngineException {
    Attr stepNode = null;
    String nodeValue = nodeText;
    if (evaluated != null) {
        if (evaluated instanceof NodeList) {
            NodeList list = (NodeList) evaluated;
            nodeValue = list.toString();
        } else if (evaluated instanceof Collection) {
            List<String> list = GenericUtils.toString((Collection<?>) evaluated);
            nodeValue = list.toString();
        } else if (evaluated instanceof NativeJavaArray) {
            Object object = ((NativeJavaArray) evaluated).unwrap();
            List<String> list = GenericUtils.toString(Arrays.asList((Object[]) object));
            nodeValue = list.toString();
        } else if (evaluated.getClass().isArray()) {
            nodeValue = Arrays.toString((Object[]) evaluated);
        } else
            nodeValue = evaluated.toString();
    }
    Document doc = getOutputDocument();
    if (!inError() && isOutput()) {
        String namespace = getNodeNameSpace();
        if (namespace.equals("")) {
            stepNode = doc.createAttribute(getStepNodeName());
            stepNode.setNodeValue(nodeValue);
        } else {
            String namespaceURI = getNodeNameSpaceURI();
            if (namespaceURI.equals(""))
                throw new EngineException("Blank namespace URI is not allowed (using namespace '" + namespace + "' in jAttribute step '" + getName() + "')");
            stepNode = doc.createAttributeNS(namespaceURI, namespace + ":" + getStepNodeName());
            stepNode.setNodeValue(nodeValue);
        }
    }
    return stepNode;
}
Also used : NodeList(org.w3c.dom.NodeList) EngineException(com.twinsoft.convertigo.engine.EngineException) Collection(java.util.Collection) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) NodeList(org.w3c.dom.NodeList) List(java.util.List) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) Document(org.w3c.dom.Document) Attr(org.w3c.dom.Attr)

Example 3 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray in project convertigo by convertigo.

the class RequestableStep method getPostQuery.

protected String getPostQuery(Scriptable scope) throws EngineException {
    StepVariable stepVariable;
    String postQuery = "";
    int len = numberOfVariables();
    String variableName;
    int variableVisibility;
    for (int i = 0; i < len; i++) {
        stepVariable = (StepVariable) getVariable(i);
        variableName = stepVariable.getName();
        variableVisibility = stepVariable.getVisibility();
        try {
            // Source value
            Object variableValue = stepVariable.getSourceValue();
            if (variableValue instanceof NodeList && ((NodeList) variableValue).getLength() == 0) {
                if (getProject().isStrictMode() && !stepVariable.isMultiValued()) {
                    // override with null (fix #24)
                    variableValue = null;
                }
            }
            if (variableValue != null) {
                Engine.logBeans.trace("(RequestableStep) found value from source: " + Visibility.Logs.printValue(variableVisibility, variableValue));
            }
            // Otherwise Scope parameter
            if (variableValue == null) {
                Scriptable searchScope = scope;
                while ((variableValue == null) && (searchScope != null)) {
                    variableValue = searchScope.get(variableName, searchScope);
                    Engine.logBeans.trace("(RequestableStep) found value from scope: " + Visibility.Logs.printValue(variableVisibility, variableValue));
                    if (variableValue instanceof Undefined) {
                        variableValue = null;
                    } else if (variableValue instanceof UniqueTag && ((UniqueTag) variableValue).equals(UniqueTag.NOT_FOUND)) {
                        variableValue = null;
                    } else if (variableValue instanceof NativeJavaObject) {
                        variableValue = ((NativeJavaObject) variableValue).unwrap();
                    }
                    if (variableValue == null) {
                        // looks up in parent's scope
                        searchScope = searchScope.getParentScope();
                    }
                }
            }
            // Otherwise context parameter
            if (variableValue == null) {
                variableValue = (sequence.context.get(variableName) == null ? null : sequence.context.get(variableName));
                if (variableValue != null)
                    Engine.logBeans.trace("(RequestableStep) found value from context: " + Visibility.Logs.printValue(variableVisibility, variableValue));
            }
            // Otherwise sequence step default value
            if (variableValue == null) {
                variableValue = getVariableValue(variableName);
                if (variableValue != null)
                    Engine.logBeans.trace("(RequestableStep) found default value from step: " + Visibility.Logs.printValue(variableVisibility, variableValue));
            }
            // otherwise value not found
            if (variableValue == null) {
                Engine.logBeans.trace("(RequestableStep) Did not find any value for \"" + variableName + "\", ignore it");
            } else {
                if (bInternalInvoke) {
                    if (stepVariable.isMultiValued() && getProject().isStrictMode() && variableValue instanceof NodeList) {
                        String subXPath = ((StepMultiValuedVariable) stepVariable).getSubXPath();
                        if (subXPath.isEmpty()) {
                            variableValue = XMLUtils.toNodeArray((NodeList) variableValue);
                        } else {
                            TwsCachedXPathAPI xpathAPI = new TwsCachedXPathAPI(this.getProject());
                            NodeList nodeList = (NodeList) variableValue;
                            NodeList[] nodeLists = new NodeList[nodeList.getLength()];
                            for (int j = 0; j < nodeLists.length; j++) {
                                try {
                                    nodeLists[j] = xpathAPI.selectNodeList(nodeList.item(j), subXPath);
                                } catch (TransformerException e) {
                                    Engine.logBeans.debug("(RequestableStep) Failed to select subXpath", e);
                                }
                            }
                            variableValue = nodeLists;
                        }
                    }
                    request.put(variableName, variableValue);
                } else {
                    String parameterValue;
                    if (variableValue instanceof NodeList) {
                        NodeList list = (NodeList) variableValue;
                        if (list != null) {
                            if (list.getLength() == 0) {
                                // Specifies here empty multivalued variable (HTTP invoque only)
                                postQuery = addParamToPostQuery(variableName, "_empty_array_", postQuery);
                            } else {
                                for (int j = 0; j < list.getLength(); j++) {
                                    parameterValue = getNodeValue(list.item(j));
                                    postQuery = addParamToPostQuery(variableName, parameterValue, postQuery);
                                }
                            }
                        }
                    } else if (variableValue instanceof NativeJavaArray) {
                        Object object = ((NativeJavaArray) variableValue).unwrap();
                        List<String> list = GenericUtils.toString(Arrays.asList((Object[]) object));
                        if (list.size() == 0) {
                            // Specifies here empty multivalued variable (HTTP invoque only)
                            postQuery = addParamToPostQuery(variableName, "_empty_array_", postQuery);
                        } else {
                            for (String value : list) {
                                postQuery = addParamToPostQuery(variableName, value, postQuery);
                            }
                        }
                    } else if (variableValue instanceof Collection<?>) {
                        List<String> list = GenericUtils.toString((Collection<?>) variableValue);
                        if (list.size() == 0) {
                            // Specifies here empty multivalued variable (HTTP invoque only)
                            postQuery = addParamToPostQuery(variableName, "_empty_array_", postQuery);
                        } else {
                            for (String value : list) {
                                postQuery = addParamToPostQuery(variableName, value, postQuery);
                            }
                        }
                    } else if (variableValue instanceof String) {
                        parameterValue = variableValue.toString();
                        postQuery = addParamToPostQuery(variableName, parameterValue, postQuery);
                    } else {
                        parameterValue = variableValue.toString();
                        postQuery = addParamToPostQuery(variableName, parameterValue, postQuery);
                    }
                }
            }
        } catch (ClassCastException e) {
            Engine.logBeans.warn("(RequestableStep) Ignoring parameter '" + variableName + "' because its value is not a string");
        }
    }
    if (bInternalInvoke) {
        return null;
    } else {
        if (Engine.logBeans.isTraceEnabled()) {
            Engine.logBeans.trace("(RequestableStep) postQuery :" + Visibility.Logs.replaceVariables(getVariables(), postQuery));
        }
        return postQuery;
    }
}
Also used : Undefined(org.mozilla.javascript.Undefined) NodeList(org.w3c.dom.NodeList) StepVariable(com.twinsoft.convertigo.beans.variables.StepVariable) Scriptable(org.mozilla.javascript.Scriptable) NativeJavaArray(org.mozilla.javascript.NativeJavaArray) StepMultiValuedVariable(com.twinsoft.convertigo.beans.variables.StepMultiValuedVariable) UniqueTag(org.mozilla.javascript.UniqueTag) XmlSchemaCollection(org.apache.ws.commons.schema.XmlSchemaCollection) Collection(java.util.Collection) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) NodeList(org.w3c.dom.NodeList) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) TransformerException(javax.xml.transform.TransformerException) TwsCachedXPathAPI(com.twinsoft.convertigo.engine.util.TwsCachedXPathAPI)

Example 4 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray in project convertigo by convertigo.

the class ParameterUtils method toStringList.

public static List<String> toStringList(Object ob) {
    List<String> list;
    if (ob != null) {
        if (ob instanceof NativeJavaObject) {
            NativeJavaObject nativeJavaObject = (NativeJavaObject) ob;
            list = toStringList(nativeJavaObject.unwrap());
        } else if (ob instanceof NativeJavaArray) {
            Object object = ((NativeJavaArray) ob).unwrap();
            list = toStringList(object);
        } else if (ob.getClass().isArray()) {
            list = toStringList(Arrays.asList((Object[]) ob));
        } else if (ob instanceof NativeArray) {
            NativeArray array = (NativeArray) ob;
            list = new ArrayList<String>((int) array.getLength());
            for (java.util.Iterator<?> i = array.iterator(); i.hasNext(); ) {
                list.add(toString(i.next()));
            }
        } else if (ob instanceof Collection<?>) {
            Collection<?> collection = GenericUtils.cast(ob);
            list = new ArrayList<String>(collection.size());
            for (Object o : collection) {
                list.add(toString(o));
            }
        } else {
            list = Arrays.asList(toString(ob));
        }
    } else {
        list = Collections.emptyList();
    }
    return list;
}
Also used : NativeArray(org.mozilla.javascript.NativeArray) ArrayList(java.util.ArrayList) Collection(java.util.Collection) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) NativeObject(org.mozilla.javascript.NativeObject) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) NativeJavaArray(org.mozilla.javascript.NativeJavaArray)

Example 5 with NativeJavaArray

use of org.mozilla.javascript.NativeJavaArray in project convertigo by convertigo.

the class ParameterUtils method toString.

public static String toString(Object ob) {
    String parameterToString;
    if (ob != null) {
        if (ob instanceof NativeObject) {
            NativeObject nativeObject = (NativeObject) ob;
            parameterToString = nativeToString(nativeObject);
        } else if (ob instanceof NativeJavaObject) {
            NativeJavaObject nativeJavaObject = (NativeJavaObject) ob;
            parameterToString = toString(nativeJavaObject.unwrap());
        } else if (ob instanceof NativeJavaArray || ob instanceof NativeArray || ob.getClass().isArray() || ob instanceof Collection<?>) {
            parameterToString = toStringList(ob).toString();
        } else if (ob instanceof NodeList) {
            parameterToString = "";
            NodeList nl = (NodeList) ob;
            for (int i = 0; i < nl.getLength(); i++) {
                parameterToString += nodeToString(nl.item(i));
            }
        } else if (ob instanceof Node) {
            parameterToString = nodeToString((Node) ob);
        } else {
            parameterToString = ob.toString();
        }
    } else {
        parameterToString = null;
    }
    return parameterToString;
}
Also used : NativeObject(org.mozilla.javascript.NativeObject) NativeArray(org.mozilla.javascript.NativeArray) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) NativeJavaObject(org.mozilla.javascript.NativeJavaObject) NativeJavaArray(org.mozilla.javascript.NativeJavaArray)

Aggregations

NativeJavaArray (org.mozilla.javascript.NativeJavaArray)14 NativeArray (org.mozilla.javascript.NativeArray)8 NativeJavaObject (org.mozilla.javascript.NativeJavaObject)8 ArrayList (java.util.ArrayList)6 List (java.util.List)6 Collection (java.util.Collection)5 NodeList (org.w3c.dom.NodeList)4 EngineException (com.twinsoft.convertigo.engine.EngineException)3 Vector (java.util.Vector)3 XmlSchemaCollection (org.apache.ws.commons.schema.XmlSchemaCollection)3 NativeObject (org.mozilla.javascript.NativeObject)3 Scriptable (org.mozilla.javascript.Scriptable)2 Undefined (org.mozilla.javascript.Undefined)2 Node (org.w3c.dom.Node)2 ApplicationException (com.servoy.j2db.ApplicationException)1 ExitScriptException (com.servoy.j2db.ExitScriptException)1 IBasicFormManager (com.servoy.j2db.IBasicFormManager)1 IFormController (com.servoy.j2db.IFormController)1 RepositoryException (com.servoy.j2db.persistence.RepositoryException)1 ScriptMethod (com.servoy.j2db.persistence.ScriptMethod)1