Search in sources :

Example 6 with MusicXmlScoreDocFileInput

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);
}
Also used : MusicXmlScoreDocFileInput(com.xenoage.zong.desktop.io.musicxml.in.MusicXmlScoreDocFileInput) MalformedURLException(java.net.MalformedURLException) SQLException(java.sql.SQLException) JseInputStream(com.xenoage.utils.jse.io.JseInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) FileNotFoundException(java.io.FileNotFoundException) PreparedStatement(java.sql.PreparedStatement) Page(com.xenoage.zong.webserver.model.Page) ScaledPage(com.xenoage.zong.webserver.model.ScaledPage) IOException(java.io.IOException) URL(java.net.URL) FileInputStream(java.io.FileInputStream) ScoreDoc(com.xenoage.zong.documents.ScoreDoc) JseInputStream(com.xenoage.utils.jse.io.JseInputStream) Layout(com.xenoage.zong.layout.Layout) Size2f(com.xenoage.utils.math.geom.Size2f) ResultSet(java.sql.ResultSet) Doc(com.xenoage.zong.webserver.model.Doc) ScoreDoc(com.xenoage.zong.documents.ScoreDoc)

Aggregations

MusicXmlScoreDocFileInput (com.xenoage.zong.desktop.io.musicxml.in.MusicXmlScoreDocFileInput)6 ScoreDoc (com.xenoage.zong.documents.ScoreDoc)6 File (java.io.File)4 JseInputStream (com.xenoage.utils.jse.io.JseInputStream)2 PdfScoreDocFileOutput (com.xenoage.zong.desktop.io.pdf.out.PdfScoreDocFileOutput)2 JseOutputStream (com.xenoage.utils.jse.io.JseOutputStream)1 Size2f (com.xenoage.utils.math.geom.Size2f)1 MidiScoreDocFileOutput (com.xenoage.zong.desktop.io.midi.out.MidiScoreDocFileOutput)1 PngScoreDocFileOutput (com.xenoage.zong.desktop.io.png.out.PngScoreDocFileOutput)1 Layout (com.xenoage.zong.layout.Layout)1 ScoreLayout (com.xenoage.zong.musiclayout.ScoreLayout)1 ScoreLayouter (com.xenoage.zong.musiclayout.layouter.ScoreLayouter)1 Doc (com.xenoage.zong.webserver.model.Doc)1 Page (com.xenoage.zong.webserver.model.Page)1 ScaledPage (com.xenoage.zong.webserver.model.ScaledPage)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1