Package nu.xom

Examples of nu.xom.Element


    Integer nextNumber = 1;
      boolean bNewMeasure = true;
      //   if there aren't any notes in the measure,
    //  continue to use the current measure
      Elements elMeasures = elCurPart.getChildElements("measure");
      Element elLastMeasure = null;
    if (elMeasures.size() > 0)
    {  elLastMeasure = elMeasures.get(elMeasures.size()-1);
      //  get the new measure number from the last one
      Attribute elNumber = elLastMeasure.getAttribute("number");
      if (elLastMeasure.getChildElements("note").size() < 1)
        bNewMeasure = false;
      else
        nextNumber = Integer.parseInt(elNumber.getValue()) + 1;
      }
    else
    //  first measure may not have been added yet
      bNewMeasure = (elCurMeasure.getChildElements("note").size() > 0);
    }
      if (bNewMeasure)
      {  //  start the new measure
      elCurMeasure = new Element("measure");
     
      //  add the new measure number
      elCurMeasure.addAttribute(new Attribute("number",
                    Integer.toString(nextNumber)));
      }
View Full Code Here


    doNote(note, false);
    }
   
    private void doNote(Note note, boolean bChord)
    {
      Element elNote = new Element("note");
     
      if (bChord)
        elNote.appendChild(new Element("chord"));
     
      //  rest
      if (note.isRest())
      {  Element elRest = new Element("rest");
        elNote.appendChild(elRest);
      }
      else
      //  pitch
        Element elPitch = new Element("pitch");
        //  step - note letter name without sharp or flat
        Element elStep = new Element("step");
        String sPitch = Note.NOTES[note.getValue() % 12];
        int iAlter = 0;
        if (sPitch.length() > 1)
        {  iAlter = sPitch.contains("#") ? 1 : -1;
          sPitch = sPitch.substring(0,1);
        }
        elStep.appendChild(sPitch);
        elPitch.appendChild(elStep);
        //  alter - -1 = flat, 1 = sharp
        if (iAlter != 0)
        {  Element elAlter = new Element("alter");
          elAlter.appendChild(Integer.toString(iAlter));
          elPitch.appendChild(elAlter);
        }
        //  octave
        Element elOctave = new Element("octave");
        elOctave.appendChild(Integer.toString(note.getValue() / 12));
        elPitch.appendChild(elOctave);
       
        elNote.appendChild(elPitch);
      }
      //  duration
      Element elDuration = new Element("duration");
      double dDuration = note.getDecimalDuration();
      int iXMLDuration = (int)
        ((dDuration * WHOLE * MUSICXMLDIVISIONS) / QUARTER);
      elDuration.appendChild(Integer.toString(iXMLDuration));
      elNote.appendChild(elDuration);
      //  tie start/stop
      boolean bDoNotation = false;
      if (note.isStartOfTie())
      {  Element elTie = new Element("tie");
        Attribute atType = new Attribute("type", "start");
        elTie.addAttribute(atType);
        elNote.appendChild(elTie);
        bDoNotation = true;
      }
      else if (note.isEndOfTie())
      {  Element elTie = new Element("tie");
      Attribute atType = new Attribute("type", "stop");
      elTie.addAttribute(atType);
      elNote.appendChild(elTie);
        bDoNotation = true;
    }
      //  duration type
      String sDuration;
      boolean bDotted = false;
      if (dDuration == 1.0)
        sDuration = "whole";
        else if (dDuration == 0.75)
        {  sDuration = "half";
          bDotted = true;
        }
        else if (dDuration == 0.5)
          sDuration = "half";
        else if (dDuration == 0.375)
        {  sDuration = "quarter";
          bDotted = true;
        }
        else if (dDuration == 0.25)
          sDuration = "quarter";
        else if (dDuration == 0.1875)
        {  sDuration = "eighth";
          bDotted = true;
        }
        else if (dDuration == 0.125)
          sDuration = "eighth";
        else if (dDuration == 0.09375)
        {  sDuration = "16th";
          bDotted = true;
        }
        else if (dDuration == 0.0625)
          sDuration = "16th";
        else if (dDuration == 0.046875)
        {  sDuration = "32nd";
          bDotted = true;
        }
        else if (dDuration == 0.03125)
          sDuration = "32nd";
        else if (dDuration == 0.0234375)
        {  sDuration = "64th";
          bDotted = true;
        }
        else if (dDuration == 0.015625)
          sDuration = "64th";
        else if (dDuration == 0.01171875)
        {  sDuration = "128th";
          bDotted = true;
        }
        else if (dDuration == 0.0078125)
          sDuration = "128th";
        else sDuration = "/" + Double.toString(dDuration);
      Element elType = new Element("type");
      elType.appendChild(sDuration);
      elNote.appendChild(elType);
      //  dotted
      if (bDotted)
      {  Element elDot = new Element("dot");
        elNote.appendChild(elDot);
      }
      //  notations
      if (bDoNotation)
      {  Element elNotations = new Element("notations");
        if (note.isStartOfTie())
        {  Element elTied = new Element("tied");
          Attribute atStart = new Attribute("type", "start");
          elTied.addAttribute(atStart);
          elNotations.appendChild(elTied);
        }
        else if (note.isEndOfTie())
        {  Element elTied = new Element("tied");
        Attribute atStart = new Attribute("type", "stop");
        elTied.addAttribute(atStart);
        elNotations.appendChild(elTied);
      }
        elNote.appendChild(elNotations);
      }
      if (elCurMeasure == null)
View Full Code Here

     * @throws Exception if there is an error parsing the pattern
     */
 
  public void parse() throws JFugueException
  DocType docType = xomDoc.getDocType();
    Element  root = xomDoc.getRootElement();
 
    if (docType.getRootElementName().compareToIgnoreCase("score-partwise") == 0)
    {  Element partlist = root.getFirstChildElement("part-list");
      Elements parts = partlist.getChildElements();
      XMLpart[] partHeaders = new XMLpart[parts.size()];
      for (int p = 0; p < parts.size(); ++p)
      {  partHeaders[p] = new XMLpart();
        parsePartHeader(parts.get(p), partHeaders[p]);
      }
View Full Code Here

  //  ID
    Attribute ID = part.getAttribute("id");
    //  may be changed by midi-instrument below
    partHeader.ID = ID.getValue();
    //  part-name
    Element partName = part.getFirstChildElement("part-name");
    partHeader.part_name = partName.getValue();
    //  may or may not have 1 or more score-instrument and
    //  midi-instrument elements
    //  score-instruments
    int x;
    Elements scoreInsts = part.getChildElements("score-instrument");
    for (x = 0; x < scoreInsts.size(); ++x )
    {  partHeader.score_instruments += scoreInsts.get(x).getValue();
      if (x < scoreInsts.size()-1)
        partHeader.score_instruments += "~";
    }
    //  midi-instruments
    Elements midiInsts = part.getChildElements("midi-instrument");
    for (x = 0; x < midiInsts.size(); ++x )
    {  Element midi_instrument = midiInsts.get(x);
      Element midi_channel = midi_instrument.getFirstChildElement("midi-channel");
      String midiChannel = (midi_channel == null) ? "" : midi_channel.getValue();
      if (midiChannel.length() > 0)
      {  partHeader.midi_instruments += midiChannel;
        partHeader.midi_instruments += "|";
      }
      Element midi_inst = midi_instrument.getFirstChildElement("midi-name");
      String midiInst = (midi_inst == null) ? "" : midi_inst.getValue();
      if (midiInst.length() < 1)
      {  Element midi_bank = midi_instrument.getFirstChildElement("midi-bank");
        midiInst = (midi_bank == null) ? "" : midi_bank.getValue();
        if (midiInst.length() < 1)
        {  Element midi_program = midi_instrument.getFirstChildElement("program");
          midiInst = (midi_program == null) ? "" : midi_program.getValue();
        }
      }
      partHeader.midi_instruments += midiInst;
      if (x < midiInsts.size()-1)
        partHeader.midi_instruments += "~";
View Full Code Here

        }
      }
    }
    Elements measures = part.getChildElements("measure");
    for (int m = 0; m < measures.size(); ++m)
    {  Element measure = measures.get(m);
      Element attributes = measure.getFirstChildElement("attributes");

      if (attributes != null)
      {  //  default key = Cmaj
        byte key = 0, scale = 0//  scale 0 = minor, 1 = major
        Element attr = attributes.getFirstChildElement("key");
        if (attr != null)
        {  Element eKey = attr.getFirstChildElement("fifths");
          if (eKey != null)
            key = Byte.parseByte(eKey.getValue());
          Element eMode = attr.getFirstChildElement("mode");
          if (eMode != null)
          {  String mode = eMode.getValue();
            if (mode.compareToIgnoreCase("major") == 0)
              scale = 0;
            else if (mode.compareToIgnoreCase("minor") == 0)
              scale = 1;
            else
                    throw new JFugueException(JFugueException.KEYSIG_EXC, mode);
          }
        }
        else
          scale = 0//  default = major
            fireKeySignatureEvent(new KeySignature(key, scale));
           
            //  divisions and beats used to calculate duration when note type not present
            Element element_divisions = attributes.getFirstChildElement("divisions");
            if (element_divisions != null)
              this.divisions = Byte.valueOf(element_divisions.getValue());
            Element element_time = attributes.getFirstChildElement("time");
            if (element_time != null)
            {  Element element_beats = element_time.getFirstChildElement("beats");
              if (element_beats != null)
                this.beats = Byte.valueOf(element_beats.getValue());
            }
      }
     
          //  tempo
      Element direction = measure.getFirstChildElement("direction");
      if (direction != null)
      {  Element directionType = direction.getFirstChildElement("direction-type");
        if (directionType != null)
        {  Element metronome = directionType.getFirstChildElement("metronome");
          if (metronome != null)
          {  Element beatUnit = metronome.getFirstChildElement("beat-unit");
            String sBeatUnit = beatUnit.getValue();
            if (sBeatUnit.compareToIgnoreCase("quarter") != 0)
              throw new JFugueException(JFugueException.BEAT_UNIT_MUST_BE_QUARTER, sBeatUnit);
            Element bpm = metronome.getFirstChildElement("per-minute");
            if (bpm != null)
            {  this.setTempo(BPMtoPPM(Float.parseFloat(bpm.getValue())));
              fireTempoEvent(new Tempo(this.getTempo()));
            }
          }
        }
      }
View Full Code Here

        //  skip grace notes
        if (note.getFirstChildElement("grace") != null)
          return;
       
        Element voice = note.getFirstChildElement("voice");
        if (voice != null)
          parseVoice(p, Integer.parseInt(voice.getValue()));
       
    Element pitch = note.getFirstChildElement("pitch");
    if (pitch != null)
    {  String sStep = pitch.getFirstChildElement("step").getValue();
      switch(sStep.charAt(0))
      {  case 'C':  noteNumber = 0;    break;
        case 'D':  noteNumber = 2;    break;
        case 'E':  noteNumber = 4;    break;
        case 'F':  noteNumber = 5;    break;
        case 'G':  noteNumber = 7;    break;
        case 'A':  noteNumber = 9;    break;
        case 'B':  noteNumber = 11break;
      }
      Element Alter = pitch.getFirstChildElement("alter");
      if (Alter != null)
      {  String sAlter = Alter.getValue();
        if (sAlter != null)
        {  noteNumber += Integer.parseInt(sAlter);
          if (noteNumber > 11)
            noteNumber = 0;
          else if (noteNumber < 0)
            noteNumber = 11;
        }
      }
      Element Octave = pitch.getFirstChildElement("octave");
      if (Octave != null)
      {  String sOctave = Octave.getValue();
        if (sOctave != null)
          octaveNumber = Byte.parseByte(sOctave);
      }
     
          // Compute the actual note number, based on octave and note
          int intNoteNumber = (octaveNumber * 12) + noteNumber;
          if ( intNoteNumber > 127) {
              throw new JFugueException(JFugueException.NOTE_OCTAVE_EXC,"", Integer.toString(intNoteNumber));
          }
          noteNumber = (byte)intNoteNumber;
    }
    else
      isRest = true;
   
    //  duration
//    Element type = note.getFirstChildElement("type");
//    if (type == null)
    //  get duration from duration element rather than type element
      Element element_duration = note.getFirstChildElement("duration");
     
      decimalDuration = (element_duration == null)
              ? beats * divisions
              :  Double.parseDouble(element_duration.getValue()) / (beats * divisions);
    }
/*    else
    {  String sDuration = type.getValue();
      if (sDuration.compareToIgnoreCase("whole") == 0)
        durationNumber = 1;
      else if (sDuration.compareToIgnoreCase("half") == 0
        durationNumber = 2;
      else if (sDuration.compareToIgnoreCase("quarter") == 0)
        durationNumber = 4;
      else if (sDuration.compareToIgnoreCase("eighth") == 0)
        durationNumber = 8;
      else if (sDuration.compareToIgnoreCase("16th") == 0)
        durationNumber = 16;
      else if (sDuration.compareToIgnoreCase("32nd") == 0)
        durationNumber = 32;
      else if (sDuration.compareToIgnoreCase("64th") == 0)
        durationNumber = 64;
      else
              throw new JFugueException(JFugueException.NOTE_DURATION_EXC, "", sDuration);
      decimalDuration = 1.0 / durationNumber;
      Element element_dot = note.getFirstChildElement("dot");
      if (element_dot != null)
        decimalDuration *= 1.5;
    }
  */
    // Tempo is in PPQ (Pulses Per Quarter).  Turn that into
        // "PPW", then multiply that by durationNumber for WHQITXN notes
        double PPW = (double)this.getTempo() * 4.0; // 4 quarter notes in a whole note
        long duration = (long)(PPW * decimalDuration);
       
        Element notations = note.getFirstChildElement("notations");
        if (notations != null)
        {   //  ties
          Element tied = notations.getFirstChildElement("tied");
          if (tied != null)
          {  Attribute tiedType = tied.getAttribute("type");
            String sTiedType = tiedType.getValue();
              if (sTiedType.compareToIgnoreCase("start") == 0)
                isStartOfTie = true;
              else if (sTiedType.compareToIgnoreCase("end") == 0)
                isEndOfTie = true;
            }
          }
            //  velocity
          Element dynamics = notations.getFirstChildElement("dynamics");
          if (dynamics != null)
          {  Node dynamic = dynamics.getChild(0);
            if (dynamic != null)
            {  for (int x = 0; x < this.volumes.length; ++x)
              {  if (dynamic.getValue().compareToIgnoreCase(this.volumes[x]) == 0)
                {  this.curVelocity = (byte)(((this.maxVelocity - this.minVelocity)
                          (this.volumes.length - 1)) * x);
                }
               
              }
            }
          }
        }
        byte attackVelocity = this.curVelocity;
        byte decayVelocity = this.curVelocity;

        // Set up the note
        if (isRest)
        {   newNote.setRest(true);
            newNote.setDuration(duration);
            newNote.setAttackVelocity( (byte)0 );          // turn off sound for rest notes
            newNote.setDecayVelocity( (byte)0 );
        }
        else
        newNote.setValue(noteNumber);
            newNote.setDuration(duration);
            newNote.setStartOfTie(isStartOfTie);
            newNote.setEndOfTie(isEndOfTie);
            newNote.setAttackVelocity(attackVelocity);
            newNote.setDecayVelocity(decayVelocity);
        }
        //  ToDo - SEQUENTIAL
        Element element_chord = note.getFirstChildElement("chord");
        newNote.setType( (element_chord == null) ? Note.FIRST : Note.PARALLEL);

/*  attempt to adjust for rounding errors in non-supported durations       
        if (newNote.getType() == Note.FIRST)
        {  if ((totalMeasurePct + decimalDuration) > 1.)
View Full Code Here

  private static final class FastStack {
    private Element[] elements = new Element[10];
    private int size = 0;
   
    public Element pop() {
      Element elem = elements[size-1];
      elements[--size] = null; // help gc
      return elem;
    }
View Full Code Here

    }
  }
 
  /** {@inheritDoc} */
  public void writeEndTag() throws IOException {
    Element elem = verifier.writeEndTag();
    if (EXPAND_EMPTY_ELEMENTS || hasChildren) {
      writeEndTag(elem); // start tag has already been written
    } else { // start tag has not yet been written
      namespaces.push();
      writeEmptyElementTag(elem); // calls writeNamespaceDeclarations(elem)
View Full Code Here

  ///////////////////////////////////////////////////////////////////////////////

  /** Voodoo workaround because Serializer.escaper.reset() isn't public */
  private static final class EmptyDocument extends Document {
    private EmptyDocument() {
      super(new Element("dummy"));
    }
View Full Code Here

      if (namespaceURI.equals(XMLConstants.XMLNS_ATTRIBUTE_NS_URI)) {
        return getSingletonIterator(XMLConstants.XMLNS_ATTRIBUTE);
      }
     
      ArrayList prefixes = new ArrayList(1);
      Element elem = element;
      do {
        int count = elem.getNamespaceDeclarationCount();
        for (int i = 0; i < count; i++) {
          String prefix = elem.getNamespacePrefix(i);
          String uri = elem.getNamespaceURI(prefix);
          if (namespaceURI.equals(uri)) {
            if (!prefixes.contains(prefix)) {
              prefixes.add(prefix);
            }
          }
        }
       
        ParentNode parent = elem.getParent();
        elem = (parent instanceof Element ? (Element) parent : null);
      } while (elem != null); // walk towards the root

      return Collections.unmodifiableList(prefixes).iterator();
    }
View Full Code Here

TOP

Related Classes of nu.xom.Element

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.