public void startElement()

in blocks/cocoon-midi/cocoon-midi-impl/src/main/java/org/apache/cocoon/serialization/XMidiSerializer.java [76:336]


    public void startElement(
        String namespaceURI,
        String localName,
        String qName,
        Attributes atts)
        throws SAXException {
        try {
            if (localName.equals("XMidi")) {
                if (state != OUTSIDE_XMIDI) {
                    throw new SAXException("XMidi element not expected here");
                }
                state = INSIDE_XMIDI;
                String version = atts.getValue("VERSION");
                if (version == null) {
                    throw new SAXException("XMidi element has no version attribute");
                } else if (!version.equals(Constants.VERSION)) {
                    throw new SAXException(
                        "XMidi element has wrong version: expecting "
                            + Constants.VERSION
                            + ", got "
                            + version);
                }
                this.getLogger().debug(
                    "Found XMidi element, version " + version);
            } else if (localName.equals("CHUNK")) {
                if (state != INSIDE_XMIDI) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                state = INSIDE_CHUNK;
                writeString(atts.getValue("TYPE"), 4);
                Integer iLen = new Integer(atts.getValue("LENGTH"));
                writeFullWord(iLen.intValue());
                this.getLogger().debug(
                    "chunk type is: "
                        + atts.getValue("TYPE")
                        + " with length "
                        + atts.getValue("LENGTH"));
            } else if (localName.equals("MThd")) {
                if (state != INSIDE_XMIDI) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                state = INSIDE_MTHD;
                writeString(atts.getValue("TYPE"), 4);
                writeFullWord(Utils.stringToInt(atts.getValue("LENGTH")));
                this.getLogger().debug(
                    "we have MThd chunk; len = " + atts.getValue("LENGTH"));
            } else if (localName.equals("MTrk")) {
                if (state != INSIDE_XMIDI) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                state = INSIDE_MTRK;
                writeString(atts.getValue("TYPE"), 4);
                writeFullWord(Utils.stringToInt(atts.getValue("LENGTH")));
                this.getLogger().debug(
                    "we have MTrk chunk; len = " + atts.getValue("LENGTH"));
            } else if (localName.equals("DELTA")) {
                if (state != INSIDE_MTRK) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                state = INSIDE_DELTA;
                String dtime = atts.getValue("DTIME");
                byte[] hdt = Utils.hexToBa(dtime, 4);
                byte[] dt = Utils.intToDelta(hdt);
                this.getLogger().debug(
                    "Delta: "
                        + dtime
                        + ", out = "
                        + Utils.baToHex(dt, 0, dt.length - 1));
                this.output.write(dt);

            } else if (localName.equals("STATUS")) {
                if (state != INSIDE_DELTA) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                state = INSIDE_STATUS;
                String sval = atts.getValue("SVAL");
                writeHex(sval, 1);
                String sl = atts.getValue("SLEN");
                expectedBytes = Utils.stringToInt(sl);
                this.getLogger().debug(
                    "Status: " + sval + ", len = " + expectedBytes);
                if (sval.equals("FF")) {
                    String nmd = atts.getValue("SNMT");
                    writeHex(nmd, 1);
                    byte[] hdt = Utils.intToBa(expectedBytes, 4);
                    byte[] xdt = Utils.intToDelta(hdt);
                    this.output.write(xdt);
                    if (expectedBytes == 0) {
                        preventDataWrite = true;
                    }
                    this.getLogger().debug("Non-midi: " + nmd);
                } else if (sval.equals("F0")) {
                    byte[] hdt = Utils.intToBa(Utils.stringToInt(sl), 4);
                    this.output.write(Utils.intToDelta(hdt));
                    this.getLogger().debug("Sysex");
                } else if (
                    sval.equals("F6")
                        | sval.equals("F8")
                        | sval.equals("FA")
                        | sval.equals("FB")
                        | sval.equals("FC")
                        | sval.equals("FE")) {
                    preventDataWrite = true;
                    this.getLogger().debug("no data");
                }
            } else if (localName.equals("CHANNEL")) {
                if ((state != INSIDE_DELTA) && (state != INSIDE_STATUS)) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                if (state == INSIDE_DELTA) {
                    state = INSIDE_DELTA_CHANNEL;
                } else if (state == INSIDE_STATUS) {
                    state = INSIDE_STATUS_CHANNEL;
                }
                this.getLogger().debug("Channel");
            } else if (localName.equals("NOTE_OFF")) {
                if ((state != INSIDE_DELTA_CHANNEL)
                    && (state != INSIDE_STATUS_CHANNEL)) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                String pitch = atts.getValue("PITCH");
                this.output.write(Utils.stringToInt(pitch));
                String vel = atts.getValue("VELOCITY");
                this.output.write(Utils.stringToInt(vel));
                this.getLogger().debug("Note off - " + pitch + ", " + vel);
            } else if (localName.equals("NOTE_ON")) {
                if ((state != INSIDE_DELTA_CHANNEL)
                    && (state != INSIDE_STATUS_CHANNEL)) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                String pitch = atts.getValue("PITCH");
                this.output.write(Utils.stringToInt(pitch));
                String vel = atts.getValue("VELOCITY");
                this.output.write(Utils.stringToInt(vel));
                this.getLogger().debug("Note on - " + pitch + ", " + vel);
            } else if (localName.equals("AFTER")) {
                if ((state != INSIDE_DELTA_CHANNEL)
                    && (state != INSIDE_STATUS_CHANNEL)) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                String pitch = atts.getValue("PITCH");
                this.output.write(Utils.stringToInt(pitch));
                String pres = atts.getValue("PRESSURE");
                this.output.write(Utils.stringToInt(pres));
                this.getLogger().debug("AFTER - " + pitch + ", " + pres);
            } else if (localName.equals("CONTROL")) {
                if ((state != INSIDE_DELTA_CHANNEL)
                    && (state != INSIDE_STATUS_CHANNEL)) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                String cnum = atts.getValue("NUMBER");
                this.output.write(Utils.stringToInt(cnum));
                String val = atts.getValue("VALUE");
                this.output.write(Utils.stringToInt(val));
                this.getLogger().debug("CONTROL - " + cnum + ", " + val);
            } else if (localName.equals("PROGRAM")) {
                if ((state != INSIDE_DELTA_CHANNEL)
                    && (state != INSIDE_STATUS_CHANNEL)) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                String patch = atts.getValue("NUMBER");
                this.output.write(Utils.stringToInt(patch));
                this.getLogger().debug("PATCH - " + patch);
            } else if (localName.equals("PRESSURE")) {
                if ((state != INSIDE_DELTA_CHANNEL)
                    && (state != INSIDE_STATUS_CHANNEL)) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }

                String amt = atts.getValue("AMOUNT");
                this.output.write(Utils.stringToInt(amt));
                this.getLogger().debug("PRESSURE - " + amt);
            } else if (localName.equals("WHEEL")) {
                if ((state != INSIDE_DELTA_CHANNEL)
                    && (state != INSIDE_STATUS_CHANNEL)) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }

                String amt = atts.getValue("AMOUNT");
                int a = Utils.stringToInt(amt);
                int b = a;
                int c = a;
                b &= 127;
                c >>= 7;
                this.output.write(c);
                this.output.write(b);
                this.getLogger().debug(
                    "Wheel - " + a + ": (" + c + "," + a + ")");
            } else if (localName.equals("EDATA")) {
                if ((state != INSIDE_DELTA) && (state != INSIDE_STATUS)) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                buffer = new StringBuffer();
                buffering = true;
                this.getLogger().debug("EDATA (element, not text)");
            } else if (
                localName.equals("FORMAT")
                    || localName.equals("TRACKS")
                    || localName.equals("PPNQ")) {
                if (state != INSIDE_MTHD) {
                    throw new SAXException(
                        localName
                            + " element not expected here, state = "
                            + state);
                }
                buffer = new StringBuffer();
                buffering = true;
                this.getLogger().debug(localName + " element");
            } else {
                this.getLogger().debug(
                    "Found " + localName + ", in state " + state);
            }

        } catch (ProcessingException e) {
            throw new SAXException(e);
        } catch (IOException e) {
            throw new SAXException(e);
        }
    }