Package com.acelet.s.scheduler

Source Code of com.acelet.s.scheduler.HolidaysObject

/* Copyright 1999-2008 Acelet.org. All rights reserved. GPL v2 license */
/** @author Wei Jiang */

package com.acelet.s.scheduler;

import java.util.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

import org.w3c.dom.*;
import org.xml.sax.InputSource;

import com.acelet.lib.Phrase;
import com.acelet.lib.EasyXml;
import com.acelet.s.task.Holiday;



public class HolidaysObject extends EasyXml {
  static final String DELIMITER = ";";
  static final int version = 1;
  public Vector holidayVector = new Vector();

  public HolidaysObject() {
  }

  String makeDelimitedString(String[] theArray) {
    if (theArray == null)
      return "";

    StringBuffer buffer = new StringBuffer("");
    for (int i = 0; i < theArray.length; i++) {
      buffer.append(theArray[i]);
      if (i < (theArray.length - 1))
        buffer.append(DELIMITER);
    }

    return buffer.toString();
  }

  public Document makeDocument() throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder.newDocument();

    Node rootNode = document.createElement("SuperHolidaysObject");
    document.appendChild(rootNode);

    for (int i = 0; i < holidayVector.size(); i++) {
      Element holidayElement = document.createElement("HolidaysObject");
      rootNode.appendChild(holidayElement);
      makeHolidayElement(document, holidayElement, (Holiday) holidayVector.elementAt(i));
    }
    return document;
  }

  void makeHolidayElement(Document document, Element holidayElement, Holiday holiday)
  throws Exception {
    appendTextNode(document, holidayElement, "version", version + "");
    appendTextNode(document, holidayElement, "setName", holiday.setName);
    appendTextNode(document, holidayElement, "name", holiday.name);
    appendTextNode(document, holidayElement, "theDay", holiday.theDay);
    appendTextNode(document, holidayElement, "comments", holiday.comments);
  }

  public void parseDocument(Document document) throws Exception {
    int comingVersion = -1;
    holidayVector = new Vector();

    NodeList list = document.getElementsByTagName("HolidaysObject");
    for (int len = 0; len < list.getLength(); len++) {
      Node node = list.item(len);
      Holiday holiday = new Holiday();

      NodeList children = node.getChildNodes();
      for (int i = 0; i < children.getLength(); i++) {
        Node childNode = children.item(i);
        String name = childNode.getNodeName().trim();
        String value = childNode.getNodeValue();
        if (!name.equals("#text")) {
          Node child1 = childNode.getFirstChild();
          if (child1 != null)
            value = child1.getNodeValue();
        }
        if (value == null)
          value = "";
        else
          value = value.trim();

        if (name.equals("version"))
          comingVersion = Integer.parseInt(value);
        else if (name.equals("setName"))
          holiday.setName = value;
        else if (name.equals("name"))
          holiday.name = value;
        else if (name.equals("theDay"))
          holiday.theDay = value;
        else if (name.equals("comments"))
          holiday.comments = value;
      }

      holidayVector.add(holiday);
    }
  }

  public void reset() {
    holidayVector = new Vector();
  }

  public String toString() {
    return holidayVector.toString();
  }
}
TOP

Related Classes of com.acelet.s.scheduler.HolidaysObject

TOP
Copyright © 2018 www.massapi.com. 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.