Search in sources :

Example 1 with XmlException

use of com.xenoage.utils.xml.XmlException in project Zong by Xenoage.

the class FileTypeReader method getFileType.

@MaybeNull
public static FileType getFileType(InputStream inputStream) throws IOException {
    // create buffered stream for reuse
    BufferedInputStream bis = new BufferedInputStream(inputStream);
    bis.mark();
    // read first two characters. if "PK", we have a compressed MusicXML file.
    int[] bytes = new int[] { bis.read(), bis.read() };
    if (// P, K
    bytes[0] == 80 && bytes[1] == 75) {
        return FileType.Compressed;
    }
    bis.reset();
    bis.unmark();
    // otherwise, try to parse as XML up to the root element (using StAX)
    try {
        XmlReader reader = platformUtils().createXmlReader(bis);
        if (reader.openNextChildElement()) {
            String n = reader.getElementName();
            switch(n) {
                case "score-partwise":
                    return FileType.XMLScorePartwise;
                case "score-timewise":
                    return FileType.XMLScoreTimewise;
                case "opus":
                    return FileType.XMLOpus;
            }
            reader.closeElement();
        }
    } catch (XmlException ex) {
        // unknown (no XML)
        return null;
    }
    // unknown
    return null;
}
Also used : BufferedInputStream(com.xenoage.utils.io.BufferedInputStream) XmlException(com.xenoage.utils.xml.XmlException) XmlReader(com.xenoage.utils.xml.XmlReader) MaybeNull(com.xenoage.utils.annotations.MaybeNull)

Example 2 with XmlException

use of com.xenoage.utils.xml.XmlException in project Zong by Xenoage.

the class MusicXMLDemoFilesTest method test.

private void test(boolean reload) throws Exception {
    long totalMusicXMLReadingTime = 0;
    long lastTime = 0;
    for (String dir : dirs) {
        for (File file : JseFileUtils.listFiles(new File(dir), plainMusicXMLFilenameFilter, false)) {
            System.out.println(file);
            lastTime = System.currentTimeMillis();
            XmlReader reader = new JseXmlReader(new FileInputStream(file));
            try {
                lastTime = System.currentTimeMillis();
                // load the document
                MusicXMLDocument doc = MusicXMLDocument.read(reader);
                if (reload) {
                    // write the document into memory
                    ByteArrayOutputStream bos = new ByteArrayOutputStream();
                    doc.write(new JseXmlWriter(bos));
                    bos.close();
                    // reload it from memory
                    ByteArrayInputStream in = new ByteArrayInputStream(bos.toByteArray());
                    MusicXMLDocument.read(new JseXmlReader(in));
                    in.close();
                // doc.write(new JseXmlWriter(new JseOutputStream(new File("test.xml"))));
                }
                totalMusicXMLReadingTime += (System.currentTimeMillis() - lastTime);
            } catch (XmlException ex) {
                throw new Exception("Failed for " + dir + "/" + file.getName() + ": " + ex.getMessage(), ex);
            }
        }
    }
    // print time
    System.out.println("Total time for read" + (reload ? "/write/read: " : ": ") + totalMusicXMLReadingTime + " ms");
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) XmlException(com.xenoage.utils.xml.XmlException) JseXmlWriter(com.xenoage.utils.jse.xml.JseXmlWriter) JseXmlReader(com.xenoage.utils.jse.xml.JseXmlReader) XmlReader(com.xenoage.utils.xml.XmlReader) MusicXMLDocument(com.xenoage.zong.musicxml.MusicXMLDocument) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) XmlException(com.xenoage.utils.xml.XmlException) JseXmlReader(com.xenoage.utils.jse.xml.JseXmlReader)

Example 3 with XmlException

use of com.xenoage.utils.xml.XmlException in project Zong by Xenoage.

the class AndroidXmlReader method closeElement.

@Override
public void closeElement() {
    if (cancelNextClose) {
        cancelNextClose = false;
        return;
    }
    try {
        int openChildren = 1;
        int event = reader.getEventType();
        while (event != XmlPullParser.END_DOCUMENT) {
            event = reader.next();
            if (event == XmlPullParser.START_TAG) {
                openChildren++;
            } else if (event == XmlPullParser.END_TAG) {
                openChildren--;
                if (openChildren == 0)
                    return;
            }
        }
    } catch (Exception ex) {
        throw new XmlException(ex);
    }
}
Also used : XmlException(com.xenoage.utils.xml.XmlException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) XmlDataException(com.xenoage.utils.xml.XmlDataException) XmlException(com.xenoage.utils.xml.XmlException) IOException(java.io.IOException)

Aggregations

XmlException (com.xenoage.utils.xml.XmlException)3 XmlReader (com.xenoage.utils.xml.XmlReader)2 MaybeNull (com.xenoage.utils.annotations.MaybeNull)1 BufferedInputStream (com.xenoage.utils.io.BufferedInputStream)1 JseXmlReader (com.xenoage.utils.jse.xml.JseXmlReader)1 JseXmlWriter (com.xenoage.utils.jse.xml.JseXmlWriter)1 XmlDataException (com.xenoage.utils.xml.XmlDataException)1 MusicXMLDocument (com.xenoage.zong.musicxml.MusicXMLDocument)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)1