package jp.crestmuse.cmx.filewrappers;

import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import org.w3c.dom.*;
import jp.crestmuse.cmx.xml.processors.*;
import jp.crestmuse.cmx.nodeinterfaces.*;
import jp.crestmuse.cmx.handlers.*;

public class ParseTest2 {

  public static void main(String[] args) {
    try {
      String home = System.getProperty("user.home");
      Properties p = new Properties(System.getProperties());
      p.load(new FileInputStream(new File(home + "/.java/properties")));
      System.setProperties(p);
      CMXFileWrapper f = CMXFileWrapper.readfile(args[0]);
      System.out.println(f);
      if (f instanceof MusicXMLWrapper) {
        NodeList nl = f.selectNodeList("//note");
        for (int i = 0; i < nl.getLength(); i++) {
          Node n = nl.item(i);
          Note note = Note.getInterface(n);
          System.out.println("PitchStep: " + note.pitchStep());
          System.out.println("PitchOct:  " + note.pitchOctave());
          System.out.println("duration:  " + note.duration());
        }
      } else if (f instanceof DeviationInstanceWrapper) {
        MusicXMLWrapper musicxml = 
		((DeviationInstanceWrapper)f).getTargetMusicXML();
        System.out.println(musicxml);
        musicxml.write(System.out);
        f.addLinks("//note-deviation", musicxml);
        NoteHandlerPartwise handler = new MyHandler2();
        musicxml.processNotePartwise(handler);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }      
}

  class MyHandler2 implements NoteHandlerPartwise {
    public void beginPart(Node partnode, MusicXMLWrapper wrapper) {
      System.out.println("BEGIN PART");
    }
    public void endPart(Node partnode, MusicXMLWrapper wrapper) {
      System.out.println("END PART");
    }
    public void beginMeasure(Node measurenode, Node Partnode, 
                             MusicXMLWrapper wrapper) {
    }
    public void endMeasure(Node measurenode, Node Partnode, 
                           MusicXMLWrapper wrapper) {
    }
    public void processNote(Node notenode, Node measurenode, Node partnode, 
				MusicXMLWrapper wrapper) {
      System.out.println("  NOTE");
      Node linkednode = 
          wrapper.linkmanager.getNodeLinkedTo(notenode, "note-deviation");
      if (linkednode != null) {
        NoteDeviation nd = NoteDeviation.getInterface(linkednode);
        System.out.println(nd.attack());
      }
    }
  }
