Package com.acelet.s.scheduler

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

/* 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.HolidayRule;



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

  public HolidayRulesObject() {
  }

  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("SuperHolidayRulesObject");
    document.appendChild(rootNode);

    for (int i = 0; i < holidayRuleVector.size(); i++) {
      Element holidayRuleElement = document.createElement("HolidayRulesObject");
      rootNode.appendChild(holidayRuleElement);
      makeHolidayRuleElement(document, holidayRuleElement,
       (HolidayRule) holidayRuleVector.elementAt(i));
    }
    return document;
  }

  void makeHolidayRuleElement(Document document, Element holidayRuleElement,
  HolidayRule holidayRule) throws Exception {
    appendTextNode(document, holidayRuleElement, "version", version + "");
    appendTextNode(document, holidayRuleElement, "setName", holidayRule.setName);
    appendTextNode(document, holidayRuleElement, "name", holidayRule.name);
    appendTextNode(document, holidayRuleElement, "holidaying", holidayRule.holidaying);
    appendTextNode(document, holidayRuleElement, "fixMonth", holidayRule.fixMonth + "");
    appendTextNode(document, holidayRuleElement, "fixDay", holidayRule.fixDay + "");
    appendTextNode(document, holidayRuleElement, "monthlyMonth", holidayRule.monthlyMonth + "");
    appendTextNode(document, holidayRuleElement, "monthlyWhichWeek",
      holidayRule.monthlyWhichWeek + "");
    appendTextNode(document, holidayRuleElement, "monthlyWhichWeekWeekDay",
      holidayRule.monthlyWhichWeekWeekDay + "");
    appendTextNode(document, holidayRuleElement, "comments", holidayRule.comments);
  }

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

    NodeList list = document.getElementsByTagName("HolidayRulesObject");
    for (int len = 0; len < list.getLength(); len++) {
      Node node = list.item(len);
      HolidayRule holidayRule = new HolidayRule();

      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"))
          holidayRule.setName = value;
        else if (name.equals("name"))
          holidayRule.name = value;
        else if (name.equals("holidaying"))
          holidayRule.holidaying = value;
        else if (name.equals("fixMonth"))
          holidayRule.fixMonth = Integer.parseInt(value);
        else if (name.equals("fixDay"))
          holidayRule.fixDay = Integer.parseInt(value);
        else if (name.equals("monthlyMonth"))
          holidayRule.monthlyMonth = Integer.parseInt(value);
        else if (name.equals("monthlyWhichWeek"))
          holidayRule.monthlyWhichWeek = Integer.parseInt(value);
        else if (name.equals("monthlyWhichWeekWeekDay"))
          holidayRule.monthlyWhichWeekWeekDay = Integer.parseInt(value);
        else if (name.equals("comments"))
          holidayRule.comments = value;
      }

      holidayRuleVector.add(holidayRule);
    }
  }

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

  public String toString() {
    return holidayRuleVector.toString();
  }

}
TOP

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

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.