xmlconfigurator.java
来自「JGRoups源码」· Java 代码 · 共 533 行 · 第 1/2 页
JAVA
533 行
} catch(Exception x) { if(x instanceof java.io.IOException) throw (java.io.IOException)x; else { IOException tmp=new IOException(); tmp.initCause(x); throw tmp; } } return configurator; } protected static ProtocolData parseProtocolData(Node protocol) throws java.io.IOException { try { protocol.normalize(); boolean isOverride=ELMT_PROT_OVERRIDE.equals(protocol.getNodeName()); int pos=0; NodeList children=protocol.getChildNodes(); /** * there should be 4 Element Nodes if we are not overriding * 1. protocol-name * 2. description * 3. class-name * 4. protocol-params * * If we are overriding we should have * 1. protocol-name * 2. protocol-params */ // String name=null; String clazzname=null; String desc=null; ProtocolParameter[] plist=null; for(int i=0; i < children.getLength(); i++) { if(children.item(i).getNodeType() == Node.ELEMENT_NODE) { pos++; if(isOverride && (pos == 2)) pos=4; switch(pos) { case 1: name=children.item(i).getFirstChild().getNodeValue(); break; case 2: desc=children.item(i).getFirstChild().getNodeValue(); break; case 3: clazzname=children.item(i).getFirstChild().getNodeValue(); break; case 4: plist=parseProtocolParameters((Element)children.item(i)); break; }//switch }//end if }//for if(isOverride) return new ProtocolData(name, plist); else return new ProtocolData(name, desc, clazzname, plist); } catch(Exception x) { if(x instanceof java.io.IOException) throw (java.io.IOException)x; else { IOException tmp=new IOException(); tmp.initCause(x); throw tmp; } } } protected static ProtocolParameter[] parseProtocolParameters(Element protparams) throws IOException { try { Vector v=new Vector(); protparams.normalize(); NodeList parameters=protparams.getChildNodes(); for(int i=0; i < parameters.getLength(); i++) { if(parameters.item(i).getNodeType() == Node.ELEMENT_NODE) { String pname=parameters.item(i).getAttributes().getNamedItem(ATTR_NAME).getNodeValue(); String pvalue=parameters.item(i).getAttributes().getNamedItem(ATTR_VALUE).getNodeValue(); ProtocolParameter p=new ProtocolParameter(pname, pvalue); v.addElement(p); }//end if }//for ProtocolParameter[] result=new ProtocolParameter[v.size()]; v.copyInto(result); return result; } catch(Exception x) { if(x instanceof java.io.IOException) throw (java.io.IOException)x; else { IOException tmp=new IOException(); tmp.initCause(x); throw tmp; } } } public static void main(String[] args) throws Exception { String input_file=null, output=null; XmlConfigurator conf; boolean old_format=false; for(int i=0; i < args.length; i++) { if(args[i].equals("-old")) { old_format=true; continue; } if(args[i].equals("-file")) { input_file=args[++i]; continue; } help(); return; } if(input_file != null) { InputStream input=null; try { input=new FileInputStream(new File(input_file)); } catch(Throwable t) { } if(input == null) { try { input=new URL(input_file).openStream(); } catch(Throwable t) { } } if(input == null) input=Thread.currentThread().getContextClassLoader().getResourceAsStream(input_file); if(old_format) { Configurator config=new Configurator(); String cfg=inputAsString(input); Vector tmp=config.parseConfigurations(cfg); System.out.println(dump(tmp));// conf=XmlConfigurator.getInstanceOldFormat(input);// output=conf.getProtocolStackString(true);// output=replace(output, "org.jgroups.protocols.", "");// System.out.println(getTitle(input_file));// System.out.println('\n' + output); } else { conf=XmlConfigurator.getInstance(input); String tmp=conf.getProtocolStackString(); System.out.println("\n" + tmp); } } else { log.error("no input file given"); } } /** * * @param tmp Vector of Configurator.ProtocolConfiguration * @return String (XML format) */ private static String dump(Vector tmp) { StringBuffer sb=new StringBuffer(); String indent=" "; sb.append("<config>\n"); for(Iterator it=tmp.iterator(); it.hasNext();) { Configurator.ProtocolConfiguration cfg=(Configurator.ProtocolConfiguration)it.next(); sb.append(indent).append("<").append(cfg.getProtocolName()); Properties props=cfg.getProperties(); if(props.size() == 0) { sb.append(" />\n"); } else { sb.append("\n").append(indent).append(indent); for(Iterator it2=props.entrySet().iterator(); it2.hasNext();) { Map.Entry entry=(Map.Entry)it2.next(); String key=(String)entry.getKey(); String val=(String)entry.getValue(); key=trim(key); val=trim(val); sb.append(key).append("=\"").append(val).append("\""); if(it2.hasNext()) { sb.append("\n").append(indent).append(indent); } } sb.append(" />\n"); } } sb.append("</config>\n"); return sb.toString(); } private static String trim(String val) { String retval=""; int index; val=val.trim(); while(true) { index=val.indexOf('\n'); if(index == -1) { retval+=val; break; } retval+=val.substring(0, index); val=val.substring(index+1); } return retval; } private static String inputAsString(InputStream input) throws IOException { int len=input.available(); byte[] buf=new byte[len]; input.read(buf, 0, len); return new String(buf); } private static String getTitle(String input) { StringBuffer sb=new StringBuffer(); sb.append("\n\n<!-- ************ JGroups Protocol Stack Configuration ************** -->\n"); sb.append("<!-- generated by XmlConfigurator on " + new Date() + " -->\n"); sb.append("<!-- input file: " + input + " -->"); return sb.toString(); } public static String replace(String input, final String expr, String replacement) { StringBuffer sb=new StringBuffer(); int new_index=0, index=0, len=expr.length(), input_len=input.length(); while(true) { new_index=input.indexOf(expr, index); if(new_index == -1) { sb.append(input.substring(index, input_len)); break; } sb.append(input.substring(index, new_index)); sb.append(replacement); index=new_index + len; } return sb.toString(); } static void help() { System.out.println("XmlConfigurator -file <input XML file> [-old]"); System.out.println("(-old: converts old (plain-text) input format into new XML format)"); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?