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));
}
}
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;
}
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);
}
}
}
}
}
}
}
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;
}
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;
}
Aggregations