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

📄 xmlconfigure.java

📁 java web 开发,Java Xml 编程指南书籍源码
💻 JAVA
字号:
package MyNa.xml;
import MyNa.utils.Env;
import java.util.Enumeration;
import java.net.URL;
import java.io.IOException;
import java.io.File;
import java.lang.Class;
import java.lang.reflect.Constructor;

import org.w3c.dom.Node;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.DocumentType;
import org.w3c.dom.Attr;


import org.xml.sax.SAXParseException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.sun.xml.parser.Resolver;
import com.sun.xml.tree.XmlDocument;
import com.sun.xml.tree.TreeWalker;
import org.w3c.dom.Element;
  
public class XmlConfigure {

/* not for instantiation; these are simple ways to provide the functionality
 of interface XmlConfig. Note that if a bean uses these, then each of the 
 subbeans will also use initFromEnv with an appropriately defined environment,
 and that bean's initFromEnv(Env E) should call on whatever setters are needed
 to extract information from the environment, _after_ calling on this
 static method. For instance, the XmlConfig might say

public String initFromEnv(Env E){
  String S=XmlConfigure.initFromEnv(E);
  if(S.length()>0)return S; // something wrong;
  useEnv(E);
  return "";
}
public void useEnv(Env E){ // pull properties from Env, call setters.
  setInitDefs(E);
  setDbUser(E.getStr("dbUser");
  setDbPwd(E.getStr("dbPwd");
...
}

and less directly,
public String initTopEnv(Env E){
  String S=XmlConfigure.initTopEnv(E);
  if(S.length()>0)return S; // something wrong
  initFromEnv(E);
}
in this case, the XmlConfig.initTopEnv function
first calls on the XmlConfigure.initTopEnv static function
which gives recursive structure to the Env; the
XmlConfig.initTopEnv function then calls on its
own initFromEnv function which uses the recursive Env
via the XmlConfigure recursive initFromEnv, which calls
each subbean's XmlConfig.initFromEnv function for each
point on the tree. the XmlConfig initFromEnv function
can therefore be extremely simple, as above.
  
*/

public static String initTopEnv(Env E){
  Enumeration keys=E.keys();
  while(keys.hasMoreElements()){
    String key=(String)keys.nextElement();
    if(key.indexOf('_')<0)continue;
    E.putSplit(key,E.get(key));
    E.remove(key);
    }
  return ""; // no errors reported in this version.
}
public static String initDomTree(String fileName,boolean validate,Env E){
  InputSource	input;
  XmlDocument	doc;
  if(null==fileName)return "null fileName for xml initialization";
  try {
    if(fileName.startsWith("http:"))
      input=Resolver.createInputSource(new URL(fileName),true);
    else input=Resolver.createInputSource(new File(fileName));
    doc = XmlDocument.createXmlDocument (input, validate);
    return initTopDomTree(doc,E);  
    } catch (SAXParseException err) {
      return "** Parsing error: line " + err.getLineNumber()+err;
    } catch (SAXException e) {
      return ""+e;
    } catch (IOException t) {
     return ""+t;
    }
}

public static String initFromEnv(Env E){
  String msg="";
  try{
  Enumeration vals=E.elements();
  while(vals.hasMoreElements()){
    Object ob=vals.nextElement();
    if(!(ob instanceof Env))continue;
    Env beanE=(Env)ob;
    String beanName=beanE.getStr("beanName");
    String beanClass=beanE.getStr("class");
    if(null==beanName || null==beanClass) continue;
    beanE.put("theBeanParentEnv",E);
    boolean alreadyInitialized=(null!=beanE.get("theBeanItself"));
    if(!alreadyInitialized){
      Class C=Class.forName(beanClass);
      Class[]paramTypes={};
      Object[]paramVals={};
      Constructor cons=C.getConstructor(paramTypes);
      Object theBean=cons.newInstance(paramVals);
      if(!(theBean instanceof XmlConfig))continue;
      beanE.put("theBeanItself",theBean);
      }
    msg+=((XmlConfig)ob).initFromEnv(beanE);
    }
  }catch(Exception e){msg+=e;}
  return msg;
}

public static void setNodeNameVal(Env E, Node N){
  String name=N.getNodeName();
  N=N.getFirstChild(); // nodeName=#text, nodeValue=text
  String val=N.getNodeValue();
  E.put(name,val);
}
  
public static String initTopDomTree(Node theNode,Env E){ // E is output.
  if(null==theNode)return "no tree for initialization";
  theNode=(new TreeWalker(theNode)).getNextElement("topbean");
  if(null==theNode)return "no topbean node in initialization tree";
  theNode=theNode.getFirstChild();
  initDomTree(theNode,E);
  return initFromEnv(E);
}
public static String initDomTree(Node theNode, Env E){ // E is output
  // from here on, we depend on your DTD for correctness...
  E.put("beanName",theNode.getNodeName());
  theNode=theNode.getNextSibling(); // has to be <class>xxx.yyy.zzz</class>
  setNodeNameVal(E,theNode);
  theNode=theNode.getNextSibling();
  if("useclass".equals(theNode.getNodeName())){
    setNodeNameVal(E,theNode);
    theNode=theNode.getNextSibling();
    }
  if("subbeans".equals(theNode.getNodeName())){
    Node sub=theNode.getFirstChild();
    while(null!=sub){
      Env subEnv=new Env();
      E.put(sub.getNodeName(),subEnv);
      initDomTree(sub,subEnv);
      sub=sub.getNextSibling();
      }
    theNode=theNode.getNextSibling(); // now just properties...
  }
  while(null!=theNode){
    setNodeNameVal(E,theNode);
    theNode=theNode.getNextSibling();
    }
  return ""; // may catch errors, return non-empty message.
}
    
}
 

⌨️ 快捷键说明

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