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 ParseTest {
/*
  private static Document doc;
  private static void analNode(Node node) throws TransformerException {
    System.out.println(node);
    System.out.println("TRY XPOINTER");
    NodeList remote = SimplifiedXPointerProcessor.getRemoteResource(node, doc);
    if (remote != null) {
      System.out.println("Remote Resource Found");
      for (int i = 0; i < remote.getLength(); i++) 
        System.out.println(i + ": " + remote.item(i));
    }
    NodeList nl = node.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) 
      analNode(nl.item(i));
  }

  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 = new CMXFileWrapper();
      f.readfile(args[0]);
      f.removeBlankTextNodes();
      analNode(doc = f.doc);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
*/

  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);
//        NodeList nl = f.selectNodeList("//note-deviation");
////        NodeLinkManager linkmanager = new NodeLinkManager();
//        for (int i = 0; i < nl.getLength(); i++) {
//          NoteDeviation nd = NoteDeviation.getInterface(nl.item(i));
//          NodeList nl3 = nd.note(musicxml.getDocument());
//          CMXFileWrapper.linkmanager.addLink(nl.item(i), nl3);
////          for (int j = 0; j < nl3.getLength(); j++) 
////            h.put(nl3.item(j), nl.item(i));
//          System.out.println("Note:   " + nd.note(musicxml.getDocument()));
//          System.out.println("Attack: " + nd.attack());
//          System.out.println("Release:" + nd.release());
//          System.out.println("Dynamcs:" + nd.dynamics());
//          System.out.println("EdDynmc:" + nd.endDynamics());
//        }
      f.addLinks("//note-deviation", musicxml);
      NoteHandlerPartwise handler = new MyHandler();
      musicxml.processNotePartwise(handler);
//        NodeList nl2 = musicxml.selectNodeList("//note");
//        for (int i = 0; i < nl2.getLength(); i++) 
//          System.out.println(linkmanager.getNodeLinkedTo(nl2.item(i)));
////          System.out.println(h.get(nl2.item(i)));
////          System.out.println(nl2.item(i).equals(note));
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }      
}
  class MyHandler 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());
      }
    }
  }
