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

📄 treesubst.java

📁 java xml开发指南(初学者推荐)Java Xml 编程指南书籍源码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    case Node.DOCUMENT_NODE: 
      echoChildren(theNode.getFirstChild(),out);
      return;
    case Node.DOCUMENT_TYPE_NODE: 
      DocumentType dT=(DocumentType)theNode;
      echoDTD(dT.getName(),dT.getEntities(),dT.getNotations(),out);
      return;
    case Node.ELEMENT_NODE: 
      chooseRule(theNode,out);
      return;
   case Node.ENTITY_NODE: 
       out.write("<ENTITY: "); out.write(theNode.getNodeName());
       out.write(">"); out.write(theNode.getNodeValue());
       out.write("</"); out.write(theNode.getNodeName());
       out.write(">\n");
       return;
   case Node.ENTITY_REFERENCE_NODE: 
       out.write("<ENTITY_REF: "); out.write(theNode.getNodeName());
       out.write("/>\n");
       return;
   case Node.NOTATION_NODE : 
       out.write("<NOTATION: "); out.write(theNode.getNodeName());
       out.write("/>\n");
       return;
   case Node.PROCESSING_INSTRUCTION_NODE: 
       out.write("<? "); out.write(theNode.getNodeName());
       out.write(" "); out.write(theNode.getNodeValue());
       out.write(" ?>\n");
       return;
    case Node.TEXT_NODE: 
      out.write(getKey(theNode.getNodeValue()));
      return;
    default:return;
  } 
}







// these are the five tagnames we use; changing them, e.g. to
// myna_substrow, myna_setdef &c, would cause no problem.

   static String setdefN="setdef";
   static String substrowN="substrow";
   static String substerrN="substerr";
   static String substrowlistN="substrowlist";
   static String templateN="template";

// the four attrs used are name,num,dbOperation, and enumerateThrough;
// these are only relevant as attrs of the subst and setdef tags.

   static String nameAttr="name";
   static String numAttr="num";
   static String dbOperationAttr="dbOperation";
   static String enumerateThroughAttr="enumerateThrough";

// enumerateThrough has two possible values: ResultSet(default) and StrSeq

   static String resultsetAttrVal="ResultSet";
   static String strseqAttrVal="StrSeq";


// given an Element node, we can choose what to do based on its name.

public void chooseRule(Node theNode,Writer out)
      throws TemplateException,IOException{
  String tag=theNode.getNodeName();
  lg.logIt("chooseRule; tag="+tag);
  if(setdefN.equals(tag))doSetdef(theNode,out);
  else if(substrowN.equals(tag))doSubstrow(theNode,out);
  else if(substerrN.equals(tag))doSubsterr(theNode,out);
  else if(substrowlistN.equals(tag))doSubstrowlist(theNode,out);
  else if(templateN.equals(tag))doTemplate(theNode,out);
  else doDefault(theNode,out);
}

public void doDefault(Node theNode,Writer out)
     throws TemplateException,IOException{
/* copied from original echoNode, but with subst replacing echo */
      out.write("<"); out.write(theNode.getNodeName());
      substAttributes(theNode.getAttributes(),out);
      Node kid=theNode.getFirstChild();
      if(kid==null) out.write("/>\n");
      else {
        out.write(">");
        substChildren(kid,out);
        out.write("</");
        out.write(theNode.getNodeName());
        out.write(">\n");
        }
}
public void chooseRule(Node theNode)
    throws TemplateException,IOException{
  chooseRule(theNode,out);
}

// we can extract any of the four attributes; "num" is numeric,
// with default 0;
// and "enumerateThrough" has default value "ResultSet".

public String getNameAttr(Node theNode){
  Node attr=theNode.getAttributes().getNamedItem(nameAttr);
  String R=(attr==null)?null:attr.getNodeValue();
  return R;
}
public String getdbOperationAttr(Node theNode){
  Node attr=theNode.getAttributes().getNamedItem(dbOperationAttr);
  String R=(attr==null)?null:attr.getNodeValue();
  return R;
}
public int getNumAttr(Node theNode){
  Node attr=theNode.getAttributes().getNamedItem(numAttr);
  String R=(attr==null)?null:attr.getNodeValue();
  if(null==R)return 0;
  int n=Integer.parseInt(R);
  if(n<0)return index[curIndex]; // -1 means use current index
  return n;
}
public String getEnumerateThroughAttr(Node theNode){
  Node attr=theNode.getAttributes().getNamedItem(enumerateThroughAttr);
  String R=(attr==null)?null:attr.getNodeValue();
  if(null==R)return resultsetAttrVal;
 return R;
}

public void setKeyIndex(String key,int n, String val){
  if(n==0)defs.put(key,val);   
  else {
    String[] A=defs.getStrSeq(key);
    if(A==null || A.length==0 || A.length<n) 
      throw new IndexOutOfBoundsException("invalid index "+n+"on key "+key+
          "with value ["+defs.getStr(key)+"]");
    A[n-1]=val; 
    defs.put(key,A); // in case it wasn't really a StrSeq before.
    }
}

public void doSetdef(Node theNode,Writer out) // bypass out
   throws TemplateException,IOException{
   doDBOperation(theNode); 
   StringWriter sW=new StringWriter();
   String key=getNameAttr(theNode);
   if(key==null)throw new TemplateException("No Name on setDef node");
   substChildren(theNode.getFirstChild(),sW);
   setKeyIndex(key,getNumAttr(theNode),sW.toString());
}


public void doSubstrow(Node theNode,Writer out)
   throws TemplateException,IOException{
   doDBOperation(theNode); 
  if(substFailure || null==rows ||
     !rows.next())
    {substFailure=true; return;}
  defs=rows.getRow();
  substChildren(theNode.getFirstChild(),out);
}
public void doSubsterr(Node theNode,Writer out)
   throws TemplateException,IOException{
   doDBOperation(theNode); 
  if(substFailure)substChildren(theNode.getFirstChild(),out);
}
public void doSubstrowlist(Node theNode,Writer out)
   throws TemplateException,IOException{
   doDBOperation(theNode); 
  String enumType=getEnumerateThroughAttr(theNode);
  if(strseqAttrVal.equals(enumType))
    doEnumThroughStrSeq(theNode,out);
  else if(substFailure || null==rows) return;
  else {
    while(rows.next()){
      defs=rows.getRow();
      substChildren(theNode.getFirstChild(),out);
    }
   }
}
public void doEnumThroughStrSeq(Node theNode,Writer out)
   throws TemplateException,IOException{
  String ssName=getNameAttr(theNode);
  if(ssName==null)
    throw new TemplateException("no name for strseq enumthrough");
  String[]ssVal=defs.getStrSeq(ssName);
  if(ssVal==null)
    throw new TemplateException("no val for "+ssName+" in enumthrough");
  curIndex++;
  if(index.length<=curIndex)
    throw new TemplateException("stack overflow in enumthrough at "+ssName);
  for(index[curIndex]=1;index[curIndex]<=ssVal.length;index[curIndex]++)
    substChildren(theNode.getFirstChild(),out);
  curIndex--;
}
public void doTemplate(Node theNode,Writer out) // set up root
   throws TemplateException,IOException{
   doDBOperation(theNode); 
   substChildren(theNode.getFirstChild(),out);
}
public void doDBOperation(Node theNode)throws TemplateException{
  String theOp=getKey(getdbOperationAttr(theNode));
  if(theOp==null)return;
  lg.logIt("dbOperation="+theOp);
  defs.put(dbOperationAttr,theOp);
  DBHandler theDBHandler=(DBHandler)defs.get("dbHandler");
  if(theDBHandler==null)
    throw new TemplateException("no dbhandler for op "+theOp);
  try{
    setRows(theDBHandler.getQueryRows(defs));
  }catch(Exception ex){
    throw new TemplateException("dbHandler for op "+theOp+
             "failed with Exception "+ex);
  }
}

public static void main(String[]args) throws Exception{
/* called as
    TreeSubst envDef.ini DB2Xml.xml
where envDef.ini provides the starting environment and 
DB2Xml.xml is the template file. (samples provided as 
MyNa\xml\pbdata.ini and others, with MyNa\xml\DB2Xml.xml);
output to stdout.
*/
  MyNa.utils.Logger lg=new MyNa.utils.Logger();
  lg.logIt("TS.main: "+Misc.stringArrayJoin(args,", "));
  PrintWriter out=new PrintWriter(System.out);
  if(args.length!=2){
    out.println("Usage:");
    out.println(" java MyNa.xml.TreeSubst envDef.ini DB2Xml.xml");
    out.println("where DB2Xml.xml is the template to be filled");
    out.println("and envDef.ini defines the DBHandler environment");
    out.flush();
    return;}
  Env e=new Env(args[0]);
  lg.logIt("TS.main: Env: "+e);
  DBHandler dbH = new DBHandler(e);
  e.put("dbHandler",dbH);
  TreeSubst tS=new TreeSubst();
  tS.setDefs(e); tS.setOut(out);
  XmlDocument doc=null;
  try{
    InputSource iS=Resolver.createInputSource(new File(args[1]));
    doc=XmlDocument.createXmlDocument(iS,false);
     } catch (SAXParseException err) { lg.logIt("SAXPE",err);
       out.println ("** Parsing error" 
             + ", line " + err.getLineNumber ()
             + ", uri " + err.getSystemId ());
       out.println("   " + err.getMessage ());
     } catch (SAXException err) { lg.logIt("SAXE",err);
       Exception x = err.getException ();
       ((x == null) ? err : x).printStackTrace (out);
     }catch (IOException err) {lg.logIt("IO",err); err.printStackTrace (out);}
  if (doc==null){
     out.println("failed to get XMLDoc "+args[1]);
     lg.logIt("no doc "+args[1]);
     return;
     }
   lg.logIt("got nonnull doc");
   Element theRoot = doc.getDocumentElement(); // content root
   theRoot.normalize();
   lg.logIt("TS.main doc normalized");
   tS.substNode(theRoot);
   out.flush();
}
}

⌨️ 快捷键说明

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