⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlparser.java

📁 Java/J2EE框架Jdon-Framework系统的Sample
💻 JAVA
字号:
/**
 * Copyright 2005 Jdon.com
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jdon.controller.config;

import java.io.InputStream;
import java.util.Map;
import java.util.HashMap;
import com.jdon.model.handler.HandlerMetaDef;
import com.jdon.model.mapping.ModelMapping;
import java.util.List;
import org.jdom.Document;
import java.util.Iterator;
import com.jdon.util.Debug;
import org.jdom.input.SAXBuilder;
import org.jdom.Element;
import com.jdon.util.FileLocator;
import com.jdon.bussinessproxy.meta.EJBTargetMetaDef;
import com.jdon.bussinessproxy.meta.POJOTargetMetaDef;

/**
 * by Jdom parse the jdonframework.xml
 * dom4j is faster than jdom, but this parser only be runned for
 * one time ,so parsing speed is not important.
 *
 * <p>@author <a href="mailto:banqiao@jdon.com">banq</a></p>
 * <p>@version JdonFramework 2005 v1.0</p>
 */
public class XmlParser {

  private final static String module = XmlParser.class.getName();
  private String configFileName = "com/jdon/framework/test/jdonframework.xml";
  private FileLocator fileLocator = new FileLocator();

  public Map parseModels(String configFileName) throws Exception {
    Map mps = new HashMap();
    if (configFileName == null)
      configFileName = this.configFileName;
    try {
      Debug.logVerbose(" config file locate :" + configFileName, module);
      InputStream xmlFile = fileLocator.getConfPathXmlStream(configFileName);
      Debug.logVerbose(" config file read :" + xmlFile, module);
      if (xmlFile == null)
        throw new Exception("can not locate file:" + configFileName);

      SAXBuilder builder = new SAXBuilder();
      Document doc = builder.build(xmlFile);
      Debug.logVerbose(" got mapping file ", module);
      // Get the root element
      Element root = doc.getRootElement();

      List modelList = root.getChildren("models");
      Iterator i = modelList.iterator();
      while (i.hasNext()) {
        Element models = (Element) i.next();
        parseModelsConfig(models, mps);
      }
      Debug.logVerbose("<!--   Models config load finished -->", module);
    } catch (Exception ex) {
      Debug.logError(" xml parse error: " + ex, module);
      throw new Exception(ex);
    }
    return mps;

  }

  private void parseModelsConfig(Element models, Map mps) throws Exception {
    Iterator i = models.getChildren("model").iterator();
    while (i.hasNext()) {
      Element model = (Element) i.next();
      parseModelConfig(model, mps);
    }

  }

  private void parseModelConfig(Element model, Map mps) throws Exception {
    ModelMapping mp = new ModelMapping();
    mp.setKeyName(model.getAttributeValue("key"));
    mp.setClassName(model.getAttributeValue("class"));
    Debug.logVerbose(" read Config Model: " + mp.getClassName(), module);
    Debug.logVerbose(" key: " + mp.getKeyName(), module);

    Element actionFormE = model.getChild("actionForm");
    mp.setFormName(actionFormE.getAttributeValue("name"));
    Debug.logVerbose(" actionForm name: " + mp.getFormName(), module);

    Element handlerE = model.getChild("handler");
    if (handlerE != null) {
      String classAttr = handlerE.getAttributeValue("class");
      if (classAttr != null) {
        mp.setHandler(classAttr);
        Debug.logVerbose(" handler class: " + mp.getHandler(), module);
      } else {
        Debug.logVerbose(" look up ejbService in handler: ", module);
        HandlerMetaDef sm = new HandlerMetaDef();
        Element serviceE = handlerE.getChild("service");
        if (serviceE != null) {
          parseHandlerConfig(serviceE, sm);
          mp.setHandlerMetaDef(sm);
        }
      }
    }
    mps.put(mp.getFormName(), mp);
  }

  private void parseHandlerConfig(Element serviceE, HandlerMetaDef sm) throws
      Exception {
    Debug.logVerbose(" find a ejbService in handler: ", module);
    sm.setServiceRef(serviceE.getAttributeValue("ref"));
    Debug.logVerbose(" service ref: " + sm.getServiceRef(), module);

    Element getMethod = serviceE.getChild("getMethod");
    sm.setFindtMethod(getMethod.getAttributeValue("name"));
    Debug.logVerbose(" getMethod name: " + sm.getFindMethod(), module);

    Element createMethod = serviceE.getChild("createMethod");
    sm.setCreateMethod(createMethod.getAttributeValue("name"));
    Debug.logVerbose(" createMethod name: " + sm.getCreateMethod(), module);

    Element updateMethod = serviceE.getChild("updateMethod");
    sm.setUpdateMethod(updateMethod.getAttributeValue("name"));
    Debug.logVerbose(" updateMethod name: " + sm.getUpdateMethod(), module);

    Element deleteMethod = serviceE.getChild("deleteMethod");
    sm.setDeleteMethod(deleteMethod.getAttributeValue("name"));
    Debug.logVerbose(" deleteMethod name: " + sm.getDeleteMethod(), module);

  }

  public Map parseServices(String configFileName) {
    Map mps = new HashMap();
    if (configFileName == null)
      configFileName = this.configFileName;
    try {
      Debug.logVerbose("read configure file " + configFileName, module);
      InputStream xmlFile = fileLocator.getConfPathXmlStream(configFileName);
      if (xmlFile == null)
        throw new Exception("can not locate file:" + configFileName);

      SAXBuilder builder = new SAXBuilder();
      Document doc = builder.build(xmlFile);

      // Get the root element
      Element root = doc.getRootElement();

      Debug.logVerbose(" load mappings for services", module);
      List mappings = root.getChildren("services");
      Iterator i = mappings.iterator();
      while (i.hasNext()) {
        Element services = (Element) i.next();
        parseEJBServicesConfig(services, mps);
        parsePOJOServicesConfig(services, mps);
      }
      Debug.logVerbose("<!--  EJBServices load finished -->", module);
    } catch (Exception ex) {
      Debug.logError(" parseServices error: " + ex, module);
    }
    return mps;
  }

  private void parseEJBServicesConfig(Element services, Map mps) throws
      Exception {
    if (services.getChildren("ejbService") != null) {
      Iterator i = services.getChildren("ejbService").iterator();
      while (i.hasNext()) {
        Element ejbService = (Element) i.next();
        parseEJBServiceConfig(ejbService, mps);
      }
    }
  }

  private void parseEJBServiceConfig(Element ejbService, Map mps) throws
      Exception {
    String name = ejbService.getAttributeValue("name");
    Debug.logVerbose(" ejbService name=" + name, module);

    Element jndiE = ejbService.getChild("jndi");
    String jndiName = jndiE.getAttributeValue("name");

    Element ejbLobjectE = ejbService.getChild("ejbLocalObject");

    Element ejbHobjectE = ejbService.getChild("ejbHomeObject");
    Element ejbRobjectE = ejbService.getChild("ejbRemoteObject");

    EJBTargetMetaDef eJBMetaDef = null;
    if (ejbLobjectE != null) {
      eJBMetaDef = new EJBTargetMetaDef(jndiName,
                                        ejbLobjectE.getAttributeValue("class"));
      Debug.logVerbose(" jndiName=" + jndiName, module);
      Debug.logVerbose(" local=" + eJBMetaDef.getLocalName(), module);
      mps.put(name, eJBMetaDef);
    } else if ( (ejbRobjectE != null) && (ejbHobjectE != null)) {
      String homeClassName = ejbHobjectE.getAttributeValue("class");
      String remoteClassName = ejbRobjectE.getAttributeValue("class");
      eJBMetaDef = new EJBTargetMetaDef(jndiName, homeClassName,
                                        remoteClassName);
      mps.put(name, eJBMetaDef);
    } else {
      throw new Exception(
          "please config ejbLocalObject or ejbHomeObject/ejbRemoteObject ");
    }

  }

  public Map parsePOJOServices(String configFileName) {
    Map mps = new HashMap();
    if (configFileName == null)
      configFileName = this.configFileName;
    try {
      Debug.logVerbose("read configure file " + configFileName, module);
      InputStream xmlFile = fileLocator.getConfPathXmlStream(configFileName);
      if (xmlFile == null)
        throw new Exception("can not locate file:" + configFileName);

      SAXBuilder builder = new SAXBuilder();
      Document doc = builder.build(xmlFile);

      // Get the root element
      Element root = doc.getRootElement();

      Debug.logVerbose(" load mappings for services", module);
      List mappings = root.getChildren("services");
      Iterator i = mappings.iterator();
      while (i.hasNext()) {
        Element services = (Element) i.next();
        parsePOJOServicesConfig(services, mps);
      }
      Debug.logVerbose("<!--  EJBServices load finished -->", module);
    } catch (Exception ex) {
      Debug.logError(" parseServices error: " + ex, module);
    }
    return mps;
  }

  private void parsePOJOServicesConfig(Element services, Map mps) throws
      Exception {
    if (services.getChildren("pojoService") != null) {
      Iterator ii = services.getChildren("pojoService").iterator();
      while (ii.hasNext()) {
        Element pojoService = (Element) ii.next();
        parsePOJOServiceConfig(pojoService, mps);
      }
    }
  }

  private void parsePOJOServiceConfig(Element pojoService, Map mps) throws
      Exception {
    String name = pojoService.getAttributeValue("name");
    String className = pojoService.getAttributeValue("class");
    Debug.logVerbose(" pojoService name=" + name + " class=" + className,
                     module);

    POJOTargetMetaDef pojoMetaDef = new POJOTargetMetaDef(className);
    if ( (className == null) || (className.equals("")))
      throw new Exception("className is null ");

    mps.put(name, pojoMetaDef);

  }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -