Search in sources :

Example 71 with NodeList

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

the class ImportCogToolXML method parseAction.

// parseTransition
/**
     * Imports an action
     * @param node
     */
private AAction parseAction(Node node) {
    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(MOUSE_ACTION_ELT)) {
                return parseMouseAction(child);
            }
            if (nodeName.equalsIgnoreCase(TOUCHSCREEN_ACTION_ELT)) {
                return parseTouchscreenAction(child);
            }
            if (nodeName.equalsIgnoreCase(GRAFFITI_ACTION_ELT)) {
                return parseGraffitiAction(child);
            }
            if (nodeName.equalsIgnoreCase(KEYBOARD_ACTION_ELT)) {
                return parseKeyboardAction(child);
            }
            if (nodeName.equalsIgnoreCase(VOICE_ACTION_ELT)) {
                return parseVoiceAction(child);
            }
        }
    }
    return null;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 72 with NodeList

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

the class ImportCogToolXML method parseKeyboardAction.

private AAction parseKeyboardAction(Node node) {
    NodeList children = node.getChildNodes();
    if (children != null) {
        String text = null;
        int modifiers = AAction.NONE;
        KeyPressType pressType = getPressType(getAttributeValue(node, TYPE_ATTR));
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String nodeName = child.getNodeName();
            if (nodeName.equalsIgnoreCase(TEXT_ELT)) {
                text = getElementText(child);
            }
        // No need to handle modifiers any more;
        // SPECIAL CHARACTERS ARE USED INSTEAD
        }
        if (text != null) {
            boolean isCommand = isAttributeTRUE(getAttributeValue(node, IS_CMD_ATTR), true);
            if ((text.length() == 1) && isCommand) {
                return new KeyAction(text.charAt(0), pressType, modifiers);
            }
            AAction action = new KeyAction(text, isCommand, modifiers);
            addAttributes(action, node);
            return action;
        }
    }
    return null;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 73 with NodeList

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

the class ImportCogToolXML method parseFrame.

/**
     * Imports a frame
     * @param node
     */
private Frame parseFrame(Design design, Node node) throws IOException {
    NodeList children = node.getChildNodes();
    if (children != null) {
        String frameName = getAttributeValue(node, NAME_ATTR);
        if ((frameName == null) || frameName.equals("")) {
            failedObjectErrors.add("Cannot create a frame with an empty name.");
            return null;
        }
        // This adds the created frame to the design
        Frame frame = getFrame(design, frameName);
        addAttributes(frame, node);
        Frame.setFrameDevices(frame, design.getDeviceTypes());
        TransitionSource keyboardDevice = frame.getInputDevice(DeviceType.Keyboard);
        TransitionSource voiceDevice = frame.getInputDevice(DeviceType.Voice);
        // Some widgets have parents; so as not to require that
        // all widgets of a frame occur in a particular order, we must
        // resolve the parent names after all widgets have been parsed.
        // Maps the child widget to the name of its parent
        Map<ChildWidget, String> pendingParentSets = new LinkedHashMap<ChildWidget, String>();
        // Some attributes refer to widget names; must resolve these
        // after all widgets have been created.
        // Currently, the only such attribute that applies to widgets
        // is WidgetAttributes.SELECTION_ATTR
        // Maps the attributed object to the widget name that is
        // the value of the WidgetAttributes.SELECTION_ATTR attribute
        Map<IAttributed, String> pendingAttrSets = new HashMap<IAttributed, String>();
        // Some element groups may be referenced as members of other
        // groups before being defined; this map will hold them
        Map<String, FrameElementGroup> pendingGrps = new HashMap<String, FrameElementGroup>();
        // Some remote labels may not be defined before they're referenced
        // so keep track of those cases.  Maps the owner object to
        // the name of the remote label
        Map<FrameElement, String> pendingRemoteLabels = new HashMap<FrameElement, String>();
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            String nodeName = child.getNodeName();
            if (nodeName.equalsIgnoreCase(BKG_IMAGE_PATH_ELT)) {
                String backgroundImagePath = getElementText(child);
                byte[] image = loadImage(backgroundImagePath);
                if (image != null) {
                    frameLoader.set(frame, Frame.backgroundVAR, image);
                    frame.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, backgroundImagePath);
                }
            } else if (nodeName.equalsIgnoreCase(BKG_IMAGE_DATA_ELT)) {
                String backgroundImageData = getElementText(child);
                String imageName = getAttributeValue(child, NAME_ATTR);
                byte[] image = null;
                if (backgroundImageData != "") {
                    image = Base64.decode(backgroundImageData);
                    if ((imageName != null) && !imageName.equals("")) {
                        cachedImages.put(imageName, image);
                    }
                } else if ((imageName != null) && !imageName.equals("")) {
                    // If imageName specified but there is no data, trust and
                    // try to find the last image data associated with that
                    // name in the cache.
                    image = cachedImages.get(imageName);
                }
                if (image != null) {
                    frameLoader.set(frame, Frame.backgroundVAR, image);
                    if ((imageName != null) && !imageName.equals("")) {
                        frame.setAttribute(WidgetAttributes.IMAGE_PATH_ATTR, imageName);
                    }
                }
            } else if (nodeName.equalsIgnoreCase(ORIGIN_ELT)) {
                double x = Double.parseDouble(getAttributeValue(child, X_ATTR));
                double y = Double.parseDouble(getAttributeValue(child, Y_ATTR));
                DoublePoint origin = new DoublePoint(x, y);
                frameLoader.set(frame, Frame.originVAR, origin);
            } else if (nodeName.equalsIgnoreCase(SPEAKER_TEXT_ELT)) {
                frameLoader.set(frame, Frame.speakerTextVAR, getElementText(child));
            } else if (nodeName.equalsIgnoreCase(LISTEN_TIME_SECS_ELT)) {
                frameLoader.set(frame, Frame.listenTimeVAR, Double.parseDouble(getElementText(child)));
            } else if (nodeName.equalsIgnoreCase(WIDGET_ELT)) {
                IWidget w = parseWidget(design, frame, pendingParentSets, pendingAttrSets, pendingRemoteLabels, child);
                if (w != null) {
                    frame.addWidget(w);
                } else {
                    w = new Widget(null, WidgetType.Noninteractive);
                    Image wImage = GraphicsUtil.getImageFromResource("edu/cmu/cs/hcii/cogtool/resources/warning.jpg");
                    //w.setImage(wImage.getBytes());
                    frame.addWidget(w);
                }
            } else if (nodeName.equalsIgnoreCase(ELTGROUP_ELT)) {
                FrameElementGroup g = parseEltGroup(design, frame, pendingGrps, child);
                if (g != null) {
                    String eltGrpName = g.getName();
                    pendingGrps.remove(eltGrpName);
                    eltGrpName = NamedObjectUtil.makeNameUnique(eltGrpName, frame.getEltGroups());
                    g.setName(eltGrpName);
                    frame.addEltGroup(g);
                }
            } else if (nodeName.equalsIgnoreCase(KEYBOARD_TRANSITIONS_ELT)) {
                if (keyboardDevice != null) {
                    parseTransitions(design, keyboardDevice, child);
                } else {
                    failedObjectErrors.add("Keyboard transitions require that Design have a Keyboard device");
                }
            } else if (nodeName.equalsIgnoreCase(VOICE_TRANSITIONS_ELT)) {
                if (voiceDevice != null) {
                    parseTransitions(design, voiceDevice, child);
                } else {
                    failedObjectErrors.add("Voice transitions require that Design have a Voice device");
                }
            }
        }
        if (frame.getName() != null) {
            // Handle any forward references for remote labels
            Iterator<Map.Entry<FrameElement, String>> labelRefs = pendingRemoteLabels.entrySet().iterator();
            while (labelRefs.hasNext()) {
                Map.Entry<FrameElement, String> labelRef = labelRefs.next();
                setRemoteLabel(frame, labelRef.getValue(), labelRef.getKey(), null);
            }
            // If any "pending" element groups still exist, then there
            // is an error -- an element group that didn't exist!
            Iterator<FrameElementGroup> missingGrps = pendingGrps.values().iterator();
            StringBuilder errorMsg = new StringBuilder();
            while (missingGrps.hasNext()) {
                FrameElementGroup missingGrp = missingGrps.next();
                errorMsg.append("Missing widget or group, named: ");
                errorMsg.append(missingGrp.getName());
                errorMsg.append(" as member of the following groups: ");
                Iterator<FrameElementGroup> inGrps = missingGrp.getEltGroups().iterator();
                String separator = "";
                while (inGrps.hasNext()) {
                    errorMsg.append(separator + inGrps.next().getName());
                    separator = ", ";
                }
                failedObjectErrors.add(errorMsg.toString());
                errorMsg.delete(0, errorMsg.length());
            }
            Iterator<Map.Entry<ChildWidget, String>> childToParentSet = pendingParentSets.entrySet().iterator();
            // relationships
            while (childToParentSet.hasNext()) {
                Map.Entry<ChildWidget, String> childToParent = childToParentSet.next();
                String parentName = childToParent.getValue();
                if (!"".equals(parentName)) {
                    ChildWidget child = childToParent.getKey();
                    AParentWidget parent = (AParentWidget) frame.getWidget(parentName);
                    parent.addItem(child);
                    child.setParent(parent);
                }
            }
            Iterator<Map.Entry<IAttributed, String>> selnAttrToSet = pendingAttrSets.entrySet().iterator();
            // that used widget names as values.
            while (selnAttrToSet.hasNext()) {
                Map.Entry<IAttributed, String> selnAttr = selnAttrToSet.next();
                String widgetName = selnAttr.getValue();
                IWidget attrValue = "".equals(widgetName) ? null : frame.getWidget(widgetName);
                // At the moment, all occurrences that use pendingAttrSets
                // are instances of PullDownHeader for
                // WidgetAttributes.SELECTION_ATTR
                selnAttr.getKey().setAttribute(WidgetAttributes.SELECTION_ATTR, attrValue);
            }
            return frame;
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) IAttributed(edu.cmu.cs.hcii.cogtool.util.IAttributed) Node(org.w3c.dom.Node) Image(org.eclipse.swt.graphics.Image) LinkedHashMap(java.util.LinkedHashMap) NodeList(org.w3c.dom.NodeList) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap)

Example 74 with NodeList

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

the class ImportCogToolXML method parseVoiceAction.

private AAction parseVoiceAction(Node node) {
    NodeList children = node.getChildNodes();
    if (children != null) {
        for (int i = 0; i < children.getLength(); i++) {
            Node child = children.item(i);
            if (child.getNodeName().equalsIgnoreCase(TEXT_ELT)) {
                boolean isCommand = isAttributeTRUE(getAttributeValue(node, IS_CMD_ATTR), true);
                AAction action = new VoiceAction(getElementText(child), isCommand);
                addAttributes(action, node);
                return action;
            }
        }
    }
    return null;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node)

Example 75 with NodeList

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

the class BalsamiqButtonAPIConverter method parseWidgets.

/** Helper function used by importDesign.
	 * This method is used to parse the tags of each .bmml file in the directory
     * 
     * @param  document the root of the XML tree
     * @param  fileName the specified directory or file
     * @return              the newly created frame
     */
protected void parseWidgets(Node node, Frame frame) throws IOException {
    NodeList children = node.getChildNodes();
    if (node.getNodeName().equalsIgnoreCase(CONTROLS_ELT)) {
        System.out.println("controls " + children.getLength());
        /* The controls xml tag will have a list of children which are control xml tags. 
			 * A Balsamiq control is similar to a CogTool widget */
        for (int i = 0; i < children.getLength(); i++) {
            Node controlTag = children.item(i);
            String nodeName = controlTag.getNodeName();
            if (nodeName.equalsIgnoreCase(CONTROL_ELT)) {
                String balsamiqControlType = getAttributeValue(controlTag, CONTROL_TYPE_ATTR);
                String widgetTypeString = (balsamiqControlType == null) ? null : getBMMLWidgetType(balsamiqControlType);
                System.out.println("316-widgetTypeString " + widgetTypeString);
                if (widgetTypeString.equals("group")) {
                    System.out.println("319-calling bmmlgroup");
                    parseBMMLGroup(controlTag, frame, null, 0.0, 0.0);
                } else {
                    Widget widget = parseBMMLWidget(controlTag, frame, null, 0.0, 0.0);
                    if (widget != null) {
                        frame.addWidget(widget);
                    }
                }
            }
        }
    } else {
        for (int i = 0; i < children.getLength(); i++) {
            parseWidgets(children.item(i), frame);
        }
    }
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IWidget(edu.cmu.cs.hcii.cogtool.model.IWidget) Widget(edu.cmu.cs.hcii.cogtool.model.Widget)

Aggregations

NodeList (org.w3c.dom.NodeList)1806 Node (org.w3c.dom.Node)1059 Element (org.w3c.dom.Element)902 Document (org.w3c.dom.Document)636 ArrayList (java.util.ArrayList)314 DocumentBuilder (javax.xml.parsers.DocumentBuilder)268 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)208 IOException (java.io.IOException)183 NamedNodeMap (org.w3c.dom.NamedNodeMap)144 InputSource (org.xml.sax.InputSource)131 HashMap (java.util.HashMap)121 Test (org.junit.Test)117 SAXException (org.xml.sax.SAXException)117 StringReader (java.io.StringReader)106 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)100 XPath (javax.xml.xpath.XPath)99 Attr (org.w3c.dom.Attr)80 XPathExpressionException (javax.xml.xpath.XPathExpressionException)76 File (java.io.File)64 HashSet (java.util.HashSet)59