Search in sources :

Example 1 with XmlReader

use of com.xenoage.utils.xml.XmlReader 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 XmlReader

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

the class MusicXmlScoreFileInput method read.

/**
 * Creates a {@link Score} instance from the document
 * behind the given {@link InputStream}.
 * @param inputStream  the input stream with the MusicXML document
 * @param filePath     file path if known, null otherwise
 */
@Override
public Score read(InputStream stream, @MaybeNull String filePath) throws InvalidFormatException, IOException {
    // parse MusicXML file
    MxlScorePartwise score;
    try {
        XmlReader xmlReader = platformUtils().createXmlReader(stream);
        MusicXMLDocument doc = MusicXMLDocument.read(xmlReader);
        score = doc.getScore();
    } catch (XmlDataException ex) {
        // no valid MusicXML
        throw new InvalidFormatException(ex);
    } catch (Exception ex) {
        throw new IOException(ex);
    }
    return read(score, Level.LogErrors);
}
Also used : MxlScorePartwise(com.xenoage.zong.musicxml.types.MxlScorePartwise) XmlReader(com.xenoage.utils.xml.XmlReader) MusicXMLDocument(com.xenoage.zong.musicxml.MusicXMLDocument) XmlDataException(com.xenoage.utils.xml.XmlDataException) IOException(java.io.IOException) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException) IOException(java.io.IOException) XmlDataException(com.xenoage.utils.xml.XmlDataException) InvalidFormatException(com.xenoage.utils.exceptions.InvalidFormatException)

Example 3 with XmlReader

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

the class LayoutSettingsReader method read.

// private static final String file = "data/musiclayout/default.xml";
/**
 * Reads the {@link LayoutSettings} from the given input stream.
 */
public static LayoutSettings read(InputStream inputStream) throws IOException {
    ChordWidths chordWidths = null, graceChordWidths;
    Spacings spacings = null;
    float scalingClefInner = 0, scalingGrace = 0;
    float offsetMeasureStart = 0;
    float offsetBeatsMinimal = 0;
    try {
        XmlReader r = platformUtils().createXmlReader(inputStream);
        r.openNextChildElement();
        while (r.openNextChildElement()) {
            String n = r.getElementName();
            switch(n) {
                case "chordwidths":
                    // load the chord layout settings
                    chordWidths = readChordWidths(r);
                    break;
                case "spacings":
                    // load the space settings
                    spacings = readSpacings(r);
                    break;
                case "scaling":
                    // load scalings
                    while (r.openNextChildElement()) {
                        String n2 = r.getElementName();
                        if (n2.equals("clef"))
                            scalingClefInner = parseFloat(r.getAttributeNotNull("inner"));
                        else if (n2.equals("grace"))
                            scalingGrace = parseFloat(r.getAttributeNotNull("scaling"));
                        r.closeElement();
                    }
                    break;
                case "offset":
                    // load offsets
                    while (r.openNextChildElement()) {
                        String n2 = r.getElementName();
                        if (n2.equals("measure"))
                            offsetMeasureStart = parseFloat(r.getAttributeNotNull("start"));
                        else if (n2.equals("beats"))
                            offsetBeatsMinimal = parseFloat(r.getAttributeNotNull("minimal"));
                        r.closeElement();
                    }
                    break;
            }
            r.closeElement();
        }
        r.close();
    } catch (Exception ex) {
        INSTANCE.log(Companion.error("Could not read the input stream", ex));
        throw new IOException(ex);
    /*
			//default values
			durationWidths.put(fr(1, 32), 1 + 1/2f);
			durationWidths.put(fr(1, 16), 1 + 3/4f);
			durationWidths.put(fr(1, 8), 2 + 1/2f);
			durationWidths.put(fr(1, 2), 4 + 3/4f);
			widthClef = 4;
			widthSharp = 1.2f;
			widthFlat = 1f;
			widthMeasureEmpty = 8f;
			scalingClefInner = 0.75f;
			offsetMeasureStart = 1;
			offsetBeatsMinimal = 1.5f;
			*/
    }
    // compute grace chord widths
    graceChordWidths = chordWidths.scale(scalingGrace);
    return new LayoutSettings(chordWidths, graceChordWidths, spacings, scalingClefInner, scalingGrace, offsetMeasureStart, offsetBeatsMinimal);
}
Also used : LayoutSettings(com.xenoage.zong.musiclayout.settings.LayoutSettings) Spacings(com.xenoage.zong.musiclayout.settings.Spacings) SpacingsReader.readSpacings(com.xenoage.zong.io.musiclayout.SpacingsReader.readSpacings) ChordWidthsReader.readChordWidths(com.xenoage.zong.io.musiclayout.ChordWidthsReader.readChordWidths) ChordWidths(com.xenoage.zong.musiclayout.settings.ChordWidths) XmlReader(com.xenoage.utils.xml.XmlReader) IOException(java.io.IOException) IOException(java.io.IOException)

Example 4 with XmlReader

use of com.xenoage.utils.xml.XmlReader 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 5 with XmlReader

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

the class SvgSymbolReader method step2_readFile.

private void step2_readFile(InputStream stream) {
    // create xml reader
    XmlReader xmlReader = platformUtils().createXmlReader(stream);
    // read id element. it has the format "type:id", e.g.
    // "path:clef-g", or "styled:warning". If there is no ":",
    // the type "path" is used.
    // styles: path, styled, rect
    PathSymbol ret = null;
    Exception ex = null;
    try {
        if (xmlReader.openNextChildElement()) {
            String elementId = xmlReader.getAttribute("id");
            if (elementId == null || elementId.indexOf(':') == -1) {
                // no format information. use path.
                ret = SvgPathSymbolReader.read(id, xmlReader);
            } else {
                String format = elementId.split(":")[0];
                switch(format) {
                    case "path":
                        ret = SvgPathSymbolReader.read(id, xmlReader);
                        break;
                    case "rect":
                        ex = new IllegalStateException("Could not load \"" + svgFilepath + "\": \"" + format + "\" (rect is no longer supported. Convert it into a path)");
                        break;
                    case "styled":
                        ex = new IllegalStateException("Could not load \"" + svgFilepath + "\": \"" + format + "\" (currently styled symbols are not supported)");
                        break;
                    default:
                        ex = new IllegalStateException("Unknown symbol format in \"" + svgFilepath + "\": \"" + format + "\"");
                        break;
                }
            }
        }
    } catch (Exception readerEx) {
        ex = readerEx;
    } finally {
        xmlReader.close();
    }
    if (ex != null)
        result.onFailure(ex);
    else
        result.onSuccess(ret);
}
Also used : XmlReader(com.xenoage.utils.xml.XmlReader) PathSymbol(com.xenoage.zong.symbols.PathSymbol)

Aggregations

XmlReader (com.xenoage.utils.xml.XmlReader)6 IOException (java.io.IOException)3 XmlException (com.xenoage.utils.xml.XmlException)2 MusicXMLDocument (com.xenoage.zong.musicxml.MusicXMLDocument)2 MaybeNull (com.xenoage.utils.annotations.MaybeNull)1 NonNull (com.xenoage.utils.annotations.NonNull)1 InvalidFormatException (com.xenoage.utils.exceptions.InvalidFormatException)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 ChordWidthsReader.readChordWidths (com.xenoage.zong.io.musiclayout.ChordWidthsReader.readChordWidths)1 SpacingsReader.readSpacings (com.xenoage.zong.io.musiclayout.SpacingsReader.readSpacings)1 ChordWidths (com.xenoage.zong.musiclayout.settings.ChordWidths)1 LayoutSettings (com.xenoage.zong.musiclayout.settings.LayoutSettings)1 Spacings (com.xenoage.zong.musiclayout.settings.Spacings)1 MxlScorePartwise (com.xenoage.zong.musicxml.types.MxlScorePartwise)1 PathSymbol (com.xenoage.zong.symbols.PathSymbol)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1