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;
}
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);
}
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);
}
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");
}
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);
}
Aggregations