Search in sources :

Example 81 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project cogtool by cogtool.

the class DesignEditorController method createPasteAction.

protected IListenerAction createPasteAction() {
    return new AListenerAction() {

        public boolean performAction(Object prms) {
            try {
                Collection<Object> objects = CogToolClipboard.fetchCogToolObjects();
                if ((objects != null) && (objects.size() > 0)) {
                    CompoundUndoableEdit editSequence = new CompoundUndoableEdit(L10N.get("UNDO.Paste", "Paste"), DesignEditorLID.Paste);
                    Set<DeviceType> devTypes = design.getDeviceTypes();
                    int numPasted = 0;
                    Iterator<Object> objIt = objects.iterator();
                    while (objIt.hasNext()) {
                        Object o = objIt.next();
                        if (o instanceof Frame) {
                            Frame frame = (Frame) o;
                            makeFrameNameUnique(frame);
                            // Find an unoccupied starting position
                            // by cascading.
                            DoublePoint origin = frame.getFrameOrigin();
                            DesignUtil.findDistinctOrigin(design, origin, 16.0, 16.0);
                            // Union devices
                            Iterator<InputDevice> frameDevices = frame.getInputDevices().iterator();
                            while (frameDevices.hasNext()) {
                                InputDevice inputDevice = frameDevices.next();
                                DeviceType devType = inputDevice.getDeviceType();
                                if (!devTypes.contains(devType)) {
                                    DesignCmd.addDevice(design, devType);
                                }
                            }
                            Iterator<DeviceType> designDevTypes = devTypes.iterator();
                            while (designDevTypes.hasNext()) {
                                DeviceType devType = designDevTypes.next();
                                if (frame.getInputDevice(devType) == null) {
                                    frame.addInputDevice(devType);
                                }
                            }
                            addFrame(frame, editSequence);
                            numPasted++;
                        } else if (o instanceof Transition) {
                            Transition t = (Transition) o;
                            DeviceType device = t.getAction().getDefaultDeviceType();
                            if (!devTypes.contains(device)) {
                                DesignCmd.addDevice(design, device);
                            }
                            IUndoableEdit edit = DesignEditorCmd.addTransition(demoStateMgr, t);
                            editSequence.addEdit(edit);
                            numPasted++;
                        }
                    }
                    editSequence.end();
                    undoMgr.addEdit(editSequence);
                    interaction.setStatusMessage(numPasted + " " + pasteComplete);
                } else {
                    interaction.setStatusMessage(nothingPasted);
                }
            } catch (IOException e) {
                throw new RcvrClipboardException(e);
            } catch (ParserConfigurationException e) {
                throw new RcvrClipboardException(e);
            } catch (SAXException e) {
                throw new RcvrClipboardException(e);
            } catch (ClipboardUtil.ClipboardException e) {
                throw new RcvrClipboardException(e);
            }
            return true;
        }
    };
}
Also used : Frame(edu.cmu.cs.hcii.cogtool.model.Frame) InputDevice(edu.cmu.cs.hcii.cogtool.model.InputDevice) AListenerAction(edu.cmu.cs.hcii.cogtool.util.AListenerAction) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) RcvrIOException(edu.cmu.cs.hcii.cogtool.util.RcvrIOException) IOException(java.io.IOException) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) SAXException(org.xml.sax.SAXException) DeviceType(edu.cmu.cs.hcii.cogtool.model.DeviceType) ClipboardUtil(edu.cmu.cs.hcii.cogtool.util.ClipboardUtil) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) Transition(edu.cmu.cs.hcii.cogtool.model.Transition) IUndoableEdit(edu.cmu.cs.hcii.cogtool.util.IUndoableEdit) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 82 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project cogtool by cogtool.

the class FrameEditorController method createPasteAction.

// createSetFrameTemplateAction
/**
     * The paste action for inserting copied information.
     * Currently no checks are made to ensure that the paste is valid XML.
     * @return
     */
private IListenerAction createPasteAction() {
    return new AListenerAction() {

        public boolean performAction(Object prms) {
            try {
                if (CogToolClipboard.hasCogToolObjects()) {
                    // Get the XML text from the clipboard
                    Collection<Object> objects = CogToolClipboard.fetchCogToolObjects();
                    if ((objects != null) && (objects.size() > 0)) {
                        CompoundUndoableEdit editSequence = new CompoundUndoableEdit(PASTE, FrameEditorLID.Paste);
                        int numPasted = DesignEditorCmd.pasteElements(design, model, objects, demoStateMgr, editSequence);
                        if (numPasted > 0) {
                            editSequence.end();
                            undoMgr.addEdit(editSequence);
                            interaction.setStatusMessage(numPasted + " " + pasteComplete);
                        } else {
                            interaction.setStatusMessage(nothingPasted);
                        }
                    }
                    return true;
                } else {
                    interaction.setStatusMessage(nothingPasted);
                }
            } catch (IOException e) {
                throw new RcvrClipboardException(e);
            } catch (ParserConfigurationException e) {
                throw new RcvrClipboardException(e);
            } catch (SAXException e) {
                throw new RcvrClipboardException(e);
            } catch (ClipboardUtil.ClipboardException e) {
                throw new RcvrClipboardException(e);
            }
            return false;
        }
    };
}
Also used : ClipboardUtil(edu.cmu.cs.hcii.cogtool.util.ClipboardUtil) AListenerAction(edu.cmu.cs.hcii.cogtool.util.AListenerAction) RcvrClipboardException(edu.cmu.cs.hcii.cogtool.util.RcvrClipboardException) CompoundUndoableEdit(edu.cmu.cs.hcii.cogtool.util.CompoundUndoableEdit) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) DoublePoint(edu.cmu.cs.hcii.cogtool.model.DoublePoint) SAXException(org.xml.sax.SAXException)

Example 83 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project android_frameworks_base by AOSPA.

the class OMAParser method parse.

public MOTree parse(String text, String urn) throws IOException, SAXException {
    try {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        parser.parse(new InputSource(new StringReader(text)), this);
        return new MOTree(mRoot, urn);
    } catch (ParserConfigurationException pce) {
        throw new SAXException(pce);
    }
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 84 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project GNS by MobilityFirst.

the class HostConfigParser method parseFile.

private void parseFile(String filename) throws HostConfigParseException {
    String confPath = getConfPath();
    if (confPath == null) {
        return;
    }
    Document doc = null;
    try {
        InputStream is = Files.newInputStream(Paths.get(confPath, folder, filename + fileExtension));
        //InputStream is = ClassLoader.getSystemResourceAsStream(filename + ".xml");
        //File fXmlFile = new File(filename);
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        doc = dBuilder.parse(is);
    } catch (IOException | ParserConfigurationException | SAXException e) {
        throw new HostConfigParseException("Problem creating XML document " + e);
    }
    //	//optional, but recommended
    //	//read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
    //	doc.getDocumentElement().normalize();
    NodeList nList = doc.getElementsByTagName("host");
    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            String idString = eElement.getAttribute(ID);
            String hostname = eElement.getAttribute(HOSTNAME);
            String latString = eElement.getAttribute(LAT);
            String lonString = eElement.getAttribute(LON);
            if (idString.isEmpty() || hostname.isEmpty()) {
                throw new HostConfigParseException("Missing id or hostname attribute!");
            }
            //int id = Integer.parseInt(idString);
            Point2D location = null;
            if (!lonString.isEmpty() && !latString.isEmpty()) {
                location = new Point2D.Double(Double.parseDouble(lonString), Double.parseDouble(latString));
            }
            if (location == null) {
                InetAddress inetAddress = null;
                try {
                    inetAddress = InetAddress.getByName(hostname);
                } catch (UnknownHostException e) {
                    throw new HostConfigParseException("Problem looking up IP address " + e);
                }
                String ip = inetAddress.getHostAddress();
                // and take a guess at the location (lat, long) of this host
                location = GEOLocator.lookupIPLocation(ip);
            }
            hosts.add(new HostInfo(idString, hostname, location));
        }
    }
    keyname = getSingleElementAttribute(doc, KEYNAME, "name");
    username = getSingleElementAttribute(doc, USERNAME, "name");
    hostType = getSingleElementAttribute(doc, HOSTTYPE, "name");
    installPath = getSingleElementAttribute(doc, INSTALLPATH, "name");
    // if last character is a / remove it
    if (installPath != null && !installPath.isEmpty()) {
        installPath = installPath.trim();
        if (installPath.endsWith(System.getProperty("file.separator"))) {
            installPath = installPath.substring(0, installPath.length() - 1);
        }
    }
    String dataStoreTypeName = getSingleElementAttribute(doc, DATASTORE, "name");
    if (username == null) {
        // for backwards compatibility
        username = getSingleElementAttribute(doc, USERNAME_OLD, "name");
        if (username != null) {
            System.out.println("!!!Use of deprecated element tag " + USERNAME_OLD + ". Fix this!!!");
        }
    }
    if (keyname == null || username == null || hostType == null || dataStoreTypeName == null) {
        throw new HostConfigParseException("Missing " + KEYNAME + " or " + USERNAME + " or " + HOSTTYPE + " or " + DATASTORE + " tag");
    }
    dataStoreType = DataStoreType.valueOf(dataStoreTypeName);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) UnknownHostException(java.net.UnknownHostException) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Point2D(java.awt.geom.Point2D) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) InetAddress(java.net.InetAddress)

Example 85 with ParserConfigurationException

use of javax.xml.parsers.ParserConfigurationException in project intellij-community by JetBrains.

the class ImportedToGeneralTestEventsConverter method parseTestResults.

public static void parseTestResults(Reader reader, final DefaultHandler contentHandler) throws IOException {
    try {
        SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
        parser.parse(new InputSource(reader), contentHandler);
    } catch (ParserConfigurationException | SAXException e) {
        throw new IOException(e);
    } finally {
        reader.close();
    }
}
Also used : InputSource(org.xml.sax.InputSource) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Aggregations

ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1353 SAXException (org.xml.sax.SAXException)975 IOException (java.io.IOException)891 Document (org.w3c.dom.Document)710 DocumentBuilder (javax.xml.parsers.DocumentBuilder)631 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)569 Element (org.w3c.dom.Element)372 InputSource (org.xml.sax.InputSource)246 NodeList (org.w3c.dom.NodeList)226 Node (org.w3c.dom.Node)210 SAXParser (javax.xml.parsers.SAXParser)175 TransformerException (javax.xml.transform.TransformerException)163 File (java.io.File)162 InputStream (java.io.InputStream)158 SAXParserFactory (javax.xml.parsers.SAXParserFactory)137 ByteArrayInputStream (java.io.ByteArrayInputStream)129 StringReader (java.io.StringReader)117 ArrayList (java.util.ArrayList)115 DOMSource (javax.xml.transform.dom.DOMSource)109 StreamResult (javax.xml.transform.stream.StreamResult)93