Package com.ytec.jdap.reader.impl

Source Code of com.ytec.jdap.reader.impl.XmlReaderW3C

/**
*
*/
package com.ytec.jdap.reader.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.ytec.jdap.common.Constants;
import com.ytec.jdap.common.Global;
import com.ytec.jdap.model.Application;
import com.ytec.jdap.model.Server;
import com.ytec.jdap.reader.IDataReader;

/**
* <pre>
*  Title:���ݷ��� ��XML��������w3cʵ��
*  Description: ���ݷ��� ��XML��������w3cʵ��
* </pre>
*
* @author CaiJiuFa
* @version 1.00.00
*
*          <pre>
*  �޸ļ�¼
*     �޸ĺ�汾:     �޸��ˣ�  �޸�����:     �޸�����:
* </pre>
*/
public class XmlReaderW3C implements IDataReader {
  private static Document document;

  /**
   * ��ȡһ��DOM����
   *
   * @return
   * @throws Exception
   */
  private Document getDocument() throws Exception {
    synchronized (this) {
      if (document == null) {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try {
          builder = dbf.newDocumentBuilder();
          File file = new File(Global.applicationXML);
          InputStream in = new FileInputStream(file);
          document = builder.parse(in);
        } catch (Exception e) {
          throw e;
        }
      }
      return document;
    }
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * com.ytec.jdap.reader.IXmlReader#addApp(com.ytec.jdap.model.Application)
   */
  @Override
  public void addApp(Application app) throws Exception {
    Element element = getDocument().getDocumentElement();
    Element elem = getDocument().createElement(Constants.APPLICATION);
    app.fillElement(elem);
    Map<String, String> params = app.getParams();

    if (params != null && params.size() > 0) {
      for (Iterator<String> it = params.keySet().iterator(); it.hasNext();) {
        String name = it.next();
        Element param = getDocument().createElement(Constants.PARAM);
        param.setAttribute(Constants.PARAM_NAME, name);
        param.setAttribute(Constants.PARAM_VALUE, params.get(name));
        elem.appendChild(param);
      }
    }
    element.appendChild(elem);
    saveDocument();
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * com.ytec.jdap.reader.IXmlReader#modifyApp(com.ytec.jdap.model.Application
   * )
   */
  @Override
  public void modifyApp(Application application) throws Exception {
    Element element = getDocument().getDocumentElement();
    NodeList apps = element.getElementsByTagName(Constants.APPLICATION);

    if (!application.hasBrother()) {
      application.addBrother(application);
    }
    for (Iterator<Application> its = application.getBrother().iterator(); its.hasNext();) {
      Application app = (Application) its.next();

      Element elem = null;
      for (int i = 0; i < apps.getLength(); i++) {
        elem = (Element) apps.item(i);
        if (app.getId().equals(elem.getAttribute(Constants.APP_ID))) {
          break;
        }
        elem = null;
      }
      if (elem == null) {
        continue;
      }
      app.fillElement(elem);
      Map<String, String> params = app.getParams();

      if (params != null && params.size() > 0) {
        while (elem.hasChildNodes()) {// �������
          elem.removeChild(elem.getFirstChild());
        }
        for (Iterator<String> it = params.keySet().iterator(); it.hasNext();) {
          String att = it.next();
          Element param = getDocument().createElement(Constants.PARAM);
          param.setAttribute(Constants.PARAM_NAME, att);
          param.setAttribute(Constants.PARAM_VALUE, params.get(att));
          elem.appendChild(param);
        }
      }
    }

    saveDocument();
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * com.ytec.jdap.reader.IXmlReader#modifyServer(com.ytec.jdap.model.Server)
   */
  @Override
  public void modifyServer(Server server) throws Exception {
    Element element = getDocument().getDocumentElement();
    NodeList serverList = element.getElementsByTagName(Constants.SERVER);

    if (serverList != null && serverList.getLength() > 0) {
      Element node = (Element) serverList.item(0);
      server.fillElement(node);
    }
    saveDocument();
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * com.ytec.jdap.reader.IXmlReader#modifyServer(com.ytec.jdap.model.Server)
   */
  public void modifyPassword(Server server) throws Exception {
    Element element = getDocument().getDocumentElement();
    NodeList serverList = element.getElementsByTagName(Constants.SERVER);

    if (serverList != null && serverList.getLength() > 0) {
      Element ele = (Element) serverList.item(0);
      ele.setAttribute(Constants.SERVER_PASSWORD, server.getPassWord());
    }
    saveDocument();
  }

  /*
   * (non-Javadoc)
   *
   * @see
   * com.ytec.jdap.reader.IXmlReader#removeApp(com.ytec.jdap.model.Application
   * )
   */
  @Override
  public void removeApp(Application app) throws Exception {
    Element element = getDocument().getDocumentElement();
    NodeList apps = element.getElementsByTagName(Constants.APPLICATION);

    for (int i = 0; i < apps.getLength(); i++) {
      Element elem = (Element) apps.item(i);
      if (app.getId().equals(elem.getAttribute(Constants.APP_ID))) {
        element.removeChild(elem);
      }
    }
    saveDocument();
  }

  /*
   * (non-Javadoc)
   *
   * @see com.ytec.jdap.reader.IXmlReader#removeApp(java.util.List)
   */
  public void removeApp(List<String> appIds) throws Exception {
    Element element = getDocument().getDocumentElement();
    NodeList apps = element.getElementsByTagName(Constants.APPLICATION);

    for (int j = 0; j < appIds.size(); j++) {
      Application app = this.getAppById(appIds.get(j));
      for (int i = 0; i < apps.getLength(); i++) {
        Element elem = (Element) apps.item(i);
        if (app.getId().equals(elem.getAttribute(Constants.APP_ID))) {
          element.removeChild(elem);
        }
      }
    }
    saveDocument();
  }

  /*
   * (non-Javadoc)
   *
   * @see com.ytec.jdap.reader.IXmlReader#getAppList()
   */
  @Override
  public List<Application> getAppList() throws Exception {
    List<Application> appList = new ArrayList<Application>();
    Element element = getDocument().getDocumentElement();
    NodeList apps = element.getElementsByTagName(Constants.APPLICATION);

    for (int i = 0; i < apps.getLength(); i++) {
      Application application = new Application();
      Element ele = (Element) apps.item(i);
      application.setProperties(ele);
      NodeList params = ele.getElementsByTagName(Constants.PARAM);

      if (params != null && params.getLength() > 0) {
        Map<String, String> paramMap = new HashMap<String, String>();
        for (int k = 0; k < params.getLength(); k++) {
          Element param = (Element) params.item(k);
          paramMap.put(param.getAttribute(Constants.PARAM_NAME), param.getAttribute(Constants.PARAM_VALUE));
        }
        application.setParams(paramMap);
      }
      appList.add(application);
    }
    return appList;
  }

  /*
   * (non-Javadoc)
   *
   * @see com.ytec.jdap.reader.IXmlReader#getAppById(java.lang.String)
   */
  public Application getAppById(String appId) throws Exception {
    Element element = getDocument().getDocumentElement();
    NodeList apps = element.getElementsByTagName(Constants.APPLICATION);
    Application application = new Application();

    for (int i = 0; i < apps.getLength(); i++) {
      Element ele = (Element) apps.item(i);
      if (appId.equals(ele.getAttribute(Constants.APP_ID))) {
        application.setProperties(ele);
        NodeList params = ele.getElementsByTagName(Constants.PARAM);

        if (params != null && params.getLength() > 0) {
          Map<String, String> paramMap = new HashMap<String, String>();
          for (int k = 0; k < params.getLength(); k++) {
            Element param = (Element) params.item(k);
            paramMap.put(param.getAttribute(Constants.PARAM_NAME), param.getAttribute(Constants.PARAM_VALUE));
          }
          application.setParams(paramMap);
        }
      }
    }
    return application;
  }

  /*
   * (non-Javadoc)
   *
   * @see com.ytec.jdap.reader.IXmlReader#getAppByName(java.lang.String)
   */
  public Application getAppByName(String appName) throws Exception {
    Element element = getDocument().getDocumentElement();
    NodeList apps = element.getElementsByTagName(Constants.APPLICATION);
    Application application = new Application();

    for (int i = 0; i < apps.getLength(); i++) {
      Element ele = (Element) apps.item(i);
      if (appName.equals(ele.getAttribute(Constants.APP_NAME))) {
        application.setProperties(ele);
        NodeList params = ele.getElementsByTagName(Constants.PARAM);

        if (params != null && params.getLength() > 0) {
          Map<String, String> paramMap = new HashMap<String, String>();
          for (int k = 0; k < params.getLength(); k++) {
            Element param = (Element) params.item(k);
            paramMap.put(param.getAttribute(Constants.PARAM_NAME), param.getAttribute(Constants.PARAM_VALUE));
          }
          application.setParams(paramMap);
        }
      }
    }
    return application;
  }

  /*
   * (non-Javadoc)
   *
   * @see com.ytec.jdap.reader.IDataReader#getServer()
   */
  public Server getServer() throws Exception {
    Server server = new Server();
    Element element = getDocument().getDocumentElement();
    NodeList servers = element.getElementsByTagName(Constants.SERVER);

    if (servers != null && servers.getLength() > 0) {
      Element ele = (Element) servers.item(0);
      server.setProperties(ele);
    }
    return server;
  }

  /*
   * (non-Javadoc)
   *
   * @see com.ytec.jdap.reader.IDataReader#refreshData()
   */
  @Override
  public void refreshData() throws Exception {
    document = null;
  }

  /**
   * ����DOM
   *
   * @throws Exception
   */
  private void saveDocument() throws Exception {
    try {
      TransformerFactory transFactory = TransformerFactory.newInstance();
      Transformer transFormer = transFactory.newTransformer();
      Properties properties = transFormer.getOutputProperties();
      properties.setProperty(OutputKeys.INDENT, "yes");
      properties.setProperty(OutputKeys.METHOD, "xml");
      transFormer.setOutputProperties(properties);

      DOMSource domSource = new DOMSource(getDocument());
      File file = new File(Global.applicationXML);
      file.createNewFile();
      FileOutputStream out = new FileOutputStream(file);
      StreamResult xmlResult = new StreamResult(out);
      transFormer.transform(domSource, xmlResult);
      out.close();
    } catch (Exception e) {
      throw e;
    }
  }
}
TOP

Related Classes of com.ytec.jdap.reader.impl.XmlReaderW3C

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.