Search in sources :

Example 21 with SAXException

use of org.xml.sax.SAXException in project checkstyle by checkstyle.

the class PackageNamesLoader method getPackageNames.

/**
     * Returns the set of package names, compiled from all
     * checkstyle_packages.xml files found on the given class loaders
     * classpath.
     * @param classLoader the class loader for loading the
     *          checkstyle_packages.xml files.
     * @return the set of package names.
     * @throws CheckstyleException if an error occurs.
     */
public static Set<String> getPackageNames(ClassLoader classLoader) throws CheckstyleException {
    final Set<String> result;
    try {
        //create the loader outside the loop to prevent PackageObjectFactory
        //being created anew for each file
        final PackageNamesLoader namesLoader = new PackageNamesLoader();
        final Enumeration<URL> packageFiles = classLoader.getResources(CHECKSTYLE_PACKAGES);
        while (packageFiles.hasMoreElements()) {
            final URL packageFile = packageFiles.nextElement();
            InputStream stream = null;
            try {
                stream = new BufferedInputStream(packageFile.openStream());
                final InputSource source = new InputSource(stream);
                namesLoader.parseInputSource(source);
            } catch (IOException ex) {
                throw new CheckstyleException("unable to open " + packageFile, ex);
            } finally {
                Closeables.closeQuietly(stream);
            }
        }
        result = namesLoader.packageNames;
    } catch (IOException ex) {
        throw new CheckstyleException("unable to get package file resources", ex);
    } catch (ParserConfigurationException | SAXException ex) {
        throw new CheckstyleException("unable to open one of package files", ex);
    }
    return result;
}
Also used : InputSource(org.xml.sax.InputSource) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) IOException(java.io.IOException) URL(java.net.URL) SAXException(org.xml.sax.SAXException) BufferedInputStream(java.io.BufferedInputStream) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 22 with SAXException

use of org.xml.sax.SAXException in project cogtool by cogtool.

the class ObjectPersister method load.

// registerForPersistence
/**
     * Load an Object from a file.  This method assumes that all error checking
     * and prompting/correction for the validity of the file has already
     * been completed.
     *
     * @param src the File location of the saved Object
     * @return a new instantiated Object
     * @throws java.io.IOException if any file operation fails or the SAX XML
     *         parser fails
     */
public Object load(File src) throws IOException {
    // See if an object is already loaded for the given file
    String canonicalFileName = src.getCanonicalPath();
    PersistInfo info = getInfoByName(canonicalFileName);
    if (info != null) {
        return info.obj;
    }
    // Attempt to create a file of 3x size in the temp directory
    // to hold the expanded XML serialization.
    File sizeCheckFile = File.createTempFile(tmpFilePrefix, ".size", tmpDir);
    try {
        checkDiskSpace(sizeCheckFile, 3 * src.length(), "Not enough temp space on disk to open file: " + src);
    } finally {
        sizeCheckFile.delete();
    }
    // If we're here, no exception was thrown; create checkpoint directory
    File chkptFile = createTempDirectory();
    // Open file as ZipFile and decompress
    ZipFile zip = null;
    try {
        zip = new ZipFile(src);
        // Unzip the file to the checkpoint directory
        ZipUtil.unzip(zip, chkptFile);
    } catch (ZipException ex) {
        IOException newE = new IOException("load encountered zip compression error");
        newE.initCause(ex);
        throw newE;
    } finally {
        if (zip != null) {
            zip.close();
        }
    }
    // Load object from the expanded XML serialization
    ObjectLoader l = new ObjectLoader();
    Object obj = null;
    Reader reader = null;
    try {
        reader = new InputStreamReader(new FileInputStream(new File(chkptFile, PERSIST_FILE)), "UTF-8");
        Collection<?> objSet = l.load(new InputSource(reader), null);
        // There should be only one top-level object
        Iterator<?> objs = objSet.iterator();
        if (objs.hasNext()) {
            obj = objs.next();
        }
        // Register this file for future lookup, both by object
        // and by file name
        info = new PersistInfo(obj, chkptFile, src);
        objInfos.put(obj, info);
        fileInfos.put(canonicalFileName, info);
    } catch (ParserConfigurationException e) {
        IOException newE = new IOException("load encountered parser error");
        newE.initCause(e);
        throw newE;
    } catch (SAXException e) {
        IOException newE = new IOException("load encountered SAX error");
        newE.initCause(e);
        throw newE;
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
    return obj;
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) FileReader(java.io.FileReader) ZipException(java.util.zip.ZipException) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) SAXException(org.xml.sax.SAXException) ZipFile(java.util.zip.ZipFile) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ZipFile(java.util.zip.ZipFile)

Example 23 with SAXException

use of org.xml.sax.SAXException 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 24 with SAXException

use of org.xml.sax.SAXException 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 25 with SAXException

use of org.xml.sax.SAXException in project darkFunction-Editor by darkFunction.

the class JarvWriter method checkSchema.

/// TODO: Change exception type
private void checkSchema() throws IOException {
    if (this.tag == null) {
        return;
    }
    // should use this.namespace
    try {
        verifierHandler.startElement(getDefaultNamespace(), this.tag, this.tag, attrs);
        if (this.text != null) {
            verifierHandler.characters(this.text.toCharArray(), 0, this.text.length());
        }
        verifierHandler.endElement(getDefaultNamespace(), this.tag, this.tag);
    } catch (SAXException se) {
        throw new IOException("Need to wrap the SAX exception in start element: " + se);
    }
    this.attrs.clear();
    this.tag = null;
    this.text = null;
}
Also used : IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

SAXException (org.xml.sax.SAXException)1084 IOException (java.io.IOException)653 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)387 Document (org.w3c.dom.Document)258 DocumentBuilder (javax.xml.parsers.DocumentBuilder)213 InputSource (org.xml.sax.InputSource)205 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)158 InputStream (java.io.InputStream)141 Element (org.w3c.dom.Element)115 File (java.io.File)109 NodeList (org.w3c.dom.NodeList)106 SAXParseException (org.xml.sax.SAXParseException)104 Node (org.w3c.dom.Node)91 StringReader (java.io.StringReader)83 TransformerException (javax.xml.transform.TransformerException)82 ByteArrayInputStream (java.io.ByteArrayInputStream)81 SAXParser (javax.xml.parsers.SAXParser)76 ArrayList (java.util.ArrayList)71 FileInputStream (java.io.FileInputStream)64 XMLReader (org.xml.sax.XMLReader)56