Search in sources :

Example 41 with Node

use of org.w3c.dom.Node in project lucida by claritylab.

the class RuleBasedQuestionClassifier method loadRulesFile.

private void loadRulesFile(String fileName) {
    // PARSE XML RULES FILE 
    log.debug("Parsing xml rules file");
    Document rulesDocument;
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(false);
        factory.setIgnoringComments(true);
        factory.setIgnoringElementContentWhitespace(true);
        factory.setNamespaceAware(true);
        DocumentBuilder db = factory.newDocumentBuilder();
        rulesDocument = db.parse(fileName);
    } catch (Exception e) {
        throw new RuntimeException("Failed to parse XML patterns file", e);
    }
    // EXTRACT RULE DATA.
    log.debug("Loading rules");
    NodeList ruleList = rulesDocument.getElementsByTagName("RULE");
    for (int i = 0; i < ruleList.getLength(); i++) {
        Node rule = ruleList.item(i);
        if (!rule.getNodeName().equals("RULE") || rule.getNodeType() != Node.ELEMENT_NODE)
            continue;
        rules.add(new Rule((Element) rule));
    }
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 42 with Node

use of org.w3c.dom.Node in project cogtool by cogtool.

the class ImportCogToolXML method parseMouseAction.

private AAction parseMouseAction(Node node) {
    int modifiers = AAction.NONE;
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeName().equalsIgnoreCase(KBD_MODIFIER_ELT)) {
                modifiers = addKeyboardModifier(modifiers, getElementText(child));
            }
        }
    }
    String buttonAttr = getAttributeValue(node, BUTTON_ATTR);
    String actionAttr = getAttributeValue(node, ACTION_ATTR);
    MouseButtonState button = null;
    MousePressType mouseAction = null;
    if ((actionAttr == null) || actionAttr.equalsIgnoreCase(DOWNUP_ACTION)) {
        mouseAction = MousePressType.Click;
    } else if (actionAttr.equalsIgnoreCase(DOUBLE_ACTION)) {
        mouseAction = MousePressType.Double;
    } else if (actionAttr.equalsIgnoreCase(TRIPLE_ACTION)) {
        mouseAction = MousePressType.Triple;
    } else if (actionAttr.equalsIgnoreCase(DOWN_ACTION)) {
        mouseAction = MousePressType.Down;
    } else if (actionAttr.equalsIgnoreCase(UP_ACTION)) {
        mouseAction = MousePressType.Up;
    } else if (actionAttr.equalsIgnoreCase(HOVER_ACTION)) {
        mouseAction = MousePressType.Hover;
    } else {
        failedObjectErrors.add("Unknown button action: " + actionAttr);
        mouseAction = MousePressType.Click;
    }
    if (buttonAttr == null) {
        if (mouseAction != MousePressType.Hover) {
            button = MouseButtonState.Left;
        }
    } else if (buttonAttr.equalsIgnoreCase(LEFT_BUTTON)) {
        button = MouseButtonState.Left;
    } else if (buttonAttr.equalsIgnoreCase(MIDDLE_BUTTON)) {
        button = MouseButtonState.Middle;
    } else if (buttonAttr.equalsIgnoreCase(RIGHT_BUTTON)) {
        button = MouseButtonState.Right;
    } else {
        failedObjectErrors.add("Unknown button: " + buttonAttr);
        button = MouseButtonState.Left;
    }
    AAction action = new ButtonAction(button, mouseAction, modifiers);
    addAttributes(action, node);
    return action;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 43 with Node

use of org.w3c.dom.Node in project cogtool by cogtool.

the class ImportCogToolXML method addAttributes.

private static void addAttributes(IAttributed attributed, Node node, Map<IAttributed, String> pendingAttrSets) {
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        int numAttributes = attributes.getLength();
        if (numAttributes > 0) {
            if (notInitialized) {
                registerAttributes();
            }
            for (int i = 0; i < numAttributes; i++) {
                Node attributeNode = attributes.item(i);
                // Should never be null; sanity check
                if (attributeNode != null) {
                    String attribute = attributeNode.getNodeName();
                    IAttributed.AttributeDefinition<?> attrDefn = ATTRIBUTE_REGISTRY.get(attribute);
                    if (attrDefn != null) {
                        String attrName = attrDefn.attrName;
                        String attrNodeValue = attributeNode.getNodeValue();
                        if (WidgetAttributes.SELECTION_ATTR.equals(attrName)) {
                            // attrNodeValue is name of the selected widget,
                            // but the widget may not exist yet.
                            pendingAttrSets.put(attributed, attrNodeValue);
                        } else if (VALUE_REGISTRY.containsKey(attrNodeValue)) {
                            Object attrValue = VALUE_REGISTRY.get(attrNodeValue);
                            if (!NullSafe.equals(attrValue, attrDefn.defaultValue)) {
                                attributed.setAttribute(attrName, attrValue);
                            }
                        } else {
                            // Assume string value (eg, APPENDED_TEXT_ATTR)
                            attributed.setAttribute(attrName, attrNodeValue);
                        }
                    }
                }
            }
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) IAttributed(edu.cmu.cs.hcii.cogtool.util.IAttributed)

Example 44 with Node

use of org.w3c.dom.Node in project cogtool by cogtool.

the class ImportCogToolXML method parseTouchscreenAction.

private AAction parseTouchscreenAction(Node node) {
    int modifiers = AAction.NONE;
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeName().equalsIgnoreCase(KBD_MODIFIER_ELT)) {
                modifiers = addKeyboardModifier(modifiers, getElementText(child));
            }
        }
    }
    // TODO: currently, we have no place to put the modifiers; fix TapAction!
    String actionAttr = getAttributeValue(node, ACTION_ATTR);
    TapPressType tapAction = null;
    if ((actionAttr == null) || actionAttr.equalsIgnoreCase(TAP_ACTION) || actionAttr.equalsIgnoreCase(DOWNUP_ACTION)) {
        tapAction = TapPressType.Tap;
    } else if (actionAttr.equalsIgnoreCase(DOUBLE_ACTION)) {
        tapAction = TapPressType.DoubleTap;
    } else if (actionAttr.equalsIgnoreCase(TRIPLE_ACTION)) {
        tapAction = TapPressType.TripleTap;
    } else if (actionAttr.equalsIgnoreCase(DOWN_ACTION)) {
        tapAction = TapPressType.Down;
    } else if (actionAttr.equalsIgnoreCase(UP_ACTION)) {
        tapAction = TapPressType.Up;
    } else if (actionAttr.equalsIgnoreCase(HOVER_ACTION)) {
        tapAction = TapPressType.Hover;
    } else {
        failedObjectErrors.add("Unknown tap action: " + actionAttr);
        tapAction = TapPressType.Tap;
    }
    AAction action = new TapAction(tapAction);
    addAttributes(action, node);
    return action;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 45 with Node

use of org.w3c.dom.Node in project cogtool by cogtool.

the class ImportCogToolXML method parseDemoStep.

private AScriptStep parseDemoStep(Frame currentFrame, Script genScript, Node node) {
    AScriptStep newStep = null;
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String nodeName = child.getNodeName();
            if (nodeName.equalsIgnoreCase(ACTION_STEP_ELT)) {
                String targetWidgetName = getAttributeValue(child, TARGET_WIDGET_NAME_ATTR);
                if (targetWidgetName != null) {
                    IWidget actionFocus = currentFrame.getWidget(targetWidgetName);
                    newStep = parseActionStep(actionFocus, child);
                } else {
                    newStep = parseActionStep(null, child);
                }
            } else if (nodeName.equalsIgnoreCase(KEYBOARD_STEP_ELT)) {
                InputDevice kbDevice = currentFrame.getInputDevice(DeviceType.Keyboard);
                newStep = buildActionStep(kbDevice, parseKeyboardAction(child), child);
            } else if (nodeName.equalsIgnoreCase(VOICE_STEP_ELT)) {
                InputDevice voiceDevice = currentFrame.getInputDevice(DeviceType.Voice);
                newStep = buildActionStep(voiceDevice, parseVoiceAction(child), child);
            } else if (nodeName.equalsIgnoreCase(THINK_STEP_ELT)) {
                String duration = getAttributeValue(child, DURATION_ATTR);
                String thinkLabel = getAttributeValue(child, THINK_LABEL_ATTR);
                if (thinkLabel == null) {
                    thinkLabel = ThinkScriptStep.DEFAULT_THINK_LABEL;
                }
                if (duration != null) {
                    newStep = new ThinkScriptStep(currentFrame, Double.parseDouble(duration), thinkLabel);
                }
            } else if (nodeName.equalsIgnoreCase(SYS_DELAY_STEP_ELT)) {
                String duration = getAttributeValue(child, DURATION_ATTR);
                String label = getAttributeValue(child, DELAY_LABEL_ATTR);
                if (label == null) {
                    label = DelayScriptStep.DEFAULT_DELAY_LABEL;
                }
                if (duration != null) {
                    newStep = new DelayScriptStep(currentFrame, Double.parseDouble(duration), label);
                }
            } else if (nodeName.equalsIgnoreCase(LOOK_AT_STEP_ELT)) {
                String widgetName = getAttributeValue(child, LOOKAT_WIDGET_NAME_ATTR);
                if (widgetName != null) {
                    IWidget widget = currentFrame.getWidget(widgetName);
                    if (widget != null) {
                        newStep = new LookAtScriptStep(widget);
                    }
                }
            }
            if (newStep != null) {
                generateAndAppend(newStep, genScript);
                return newStep;
            }
        }
    }
    return null;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Aggregations

Node (org.w3c.dom.Node)2347 NodeList (org.w3c.dom.NodeList)1062 Element (org.w3c.dom.Element)720 Document (org.w3c.dom.Document)545 NamedNodeMap (org.w3c.dom.NamedNodeMap)333 ArrayList (java.util.ArrayList)318 DocumentBuilder (javax.xml.parsers.DocumentBuilder)202 IOException (java.io.IOException)176 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)147 Test (org.junit.Test)132 HashMap (java.util.HashMap)127 Attr (org.w3c.dom.Attr)126 SAXException (org.xml.sax.SAXException)107 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)98 HashSet (java.util.HashSet)86 InputSource (org.xml.sax.InputSource)75 XPath (javax.xml.xpath.XPath)70 List (java.util.List)67 File (java.io.File)62 ByteArrayInputStream (java.io.ByteArrayInputStream)57