use of com.xenoage.zong.desktop.io.musicxml.in.MusicXmlScoreDocFileInput in project Zong by Xenoage.
the class OpenAction method loadDocument.
/**
* Loads the {@link Doc} at the given URL and stores information about
* the score in the database, if it is not already present.
*/
public Tuple2<ScoreDoc, Doc> loadDocument(String url, @MaybeNull UUID publicID) throws SQLException {
Connection db = Webserver.instance.getDBConnection();
ScoreDoc scoreDoc;
// public ID of the document
if (publicID == null)
publicID = UUID.randomUUID();
// may not exist yet
PreparedStatement stmt = stmt(db, "SELECT public_id FROM docs WHERE public_id = ?", publicID);
ResultSet res = stmt.executeQuery();
boolean error = res.next();
stmt.close();
if (error)
throw new SQLException("A document with this public ID already exists");
// load MusicXML document
try {
// open local or remote file
InputStream inputStream;
if (URLUtils.isAbsoluteURL(url)) {
inputStream = new URL(url).openStream();
} else {
inputStream = new FileInputStream(Webserver.webPath + url);
}
MusicXmlScoreDocFileInput in = new MusicXmlScoreDocFileInput();
scoreDoc = in.read(new JseInputStream(inputStream), null);
} catch (FileNotFoundException ex) {
throw new RuntimeException("file not found");
} catch (MalformedURLException ex) {
throw new RuntimeException("invalid URL: " + url);
} catch (IOException ex) {
throw new RuntimeException("can not read from URL: " + url);
}
// register file in database, if not already known
Layout layout = scoreDoc.getLayout();
boolean isDocKnown = Database.exists(db, "docs", "url = ?", "" + url);
if (!isDocKnown) {
Database.insert(db, "docs", "url, public_id, pages, last_access", "" + url, "" + publicID, layout.getPages().size(), unixTime());
}
// read information about the document
Doc doc = Doc.fromDB(db, "" + url);
// for new documents: save information
if (!isDocKnown) {
// page information
for (int iPage : range(layout.getPages())) {
Size2f pageSize = layout.getPages().get(iPage).getFormat().getSize();
new Page(doc.id, iPage, pageSize.width, pageSize.height).insertIntoDB(db);
}
}
return t(scoreDoc, doc);
}
Aggregations