📄 xmlscriptinterpreter.java
字号:
return msgUnit; } private void checkNestedTags() { int sum = 0; if (this.inContent > 0 ) sum++; if (this.inKey > 0) sum++; if (this.inQos > 0) sum++; /* if (sum > 1) { Thread.dumpStack(); log.error(ME, "check: there is an internal error!! Mismatch with the nested tags ..."); } */ } public void endElement(String namespaceURI, String localName, String qName) { try { checkNestedTags(); if (this.inQos > 0) { appendEndOfElement(this.qos, qName); if (QOS_TAG.equals(qName) && this.inQos > 0) this.inQos--; return; } if (this.inKey > 0) { appendEndOfElement(this.key, qName); if (KEY_TAG.equals(qName) && this.inKey > 0) this.inKey--; return; } if (CONTENT_TAG.equals(qName)) { if (this.inContent > 0) this.inContent--; if (this.inContent > 0) appendEndOfElement(this.content, qName); // because nested content tags should be there (only the outher not) this.contentData.setValueRaw(this.content.toString()); return; } if (ROOT_TAG.equals(qName)) { // "xmlBlaster" this.response = new StringBuffer("\n</"); this.response.append(ROOTRESPONSE_TAG).append(">\n"); flushResponse(); this.needsRootEndTag = false; } if ("message".equals(qName)) { try { if (this.messageList == null) { throw new XmlBlasterException(glob, ErrorCode.USER_ILLEGALARGUMENT, ME, "To send multiple messages please use " + MethodName.PUBLISH_ARR + " or " + MethodName.PUBLISH_ONEWAY); } this.messageList.add(buildMsgUnit()); } catch(XmlBlasterException e) { log.severe("endElement '" + qName + "' exception occurred when trying to build message unit: " + e.getMessage()); } return; } // comes here since the end tag is not part of the content if (this.inContent > 0) appendEndOfElement(this.content, qName); if ("property".equals(qName) && this.inProperty) { this.inProperty = false; if (this.propertyName != null) { setProperty(this.propertyName, character.toString().trim()); } character.setLength(0); return; } if (this.commandsToFire.contains(qName)) { appendEndOfElement(this.character, qName); fireCommand(qName); return; } } catch (XmlBlasterException e) { if (log.isLoggable(Level.FINE)) XmlScriptInterpreter.log.fine("endElement exception occured " + e.getMessage()); if (this.needsRootEndTag) { // is </xmlBlasterResponse> missing? this.response = new StringBuffer("\n</"); this.response.append(ROOTRESPONSE_TAG).append(">\n"); try { flushResponse(); } catch (XmlBlasterException e1) { e1.printStackTrace(); } } throw new StopParseException(e); } catch (StopParseException e) { if (this.needsRootEndTag) { // is </xmlBlasterResponse> missing? this.response = new StringBuffer("\n</"); this.response.append(ROOTRESPONSE_TAG).append(">\n"); try { flushResponse(); } catch (XmlBlasterException e1) { e1.printStackTrace(); } } throw e; } } public void startCDATA() { if (log.isLoggable(Level.FINER)) XmlScriptInterpreter.log.finer("startCDATA"); this.inCDATA++; if (this.inContent == 0) this.cdata.append("<![CDATA["); } public void endCDATA() { if (log.isLoggable(Level.FINER)) { String txt = ""; if (this.qos != null) this.qos.toString(); log.finer("endCDATA: " + txt); } this.inCDATA--; if (this.inContent == 0) this.cdata.append("]]>"); if (this.inCDATA == 0) { // append on the corresponding buffer if (this.inQos > 0) this.qos.append(this.cdata); else if (this.inKey > 0) this.key.append(this.cdata); else if (this.inContent > 0) this.content.append(this.cdata); else super.character.append(this.cdata); this.cdata = new StringBuffer(); } } public String wrapForScripting(MsgUnit msgUnit, String comment) { return wrapForScripting(ROOT_TAG, msgUnit, comment); } /** * Dump the MsgUnit to XML, the dump includes the xml header (UTF-8). * <br /> * Example: * <pre> * String xml = XmlScriptInterpreter.wrapForScripting( * XmlScriptInterpreter.ROOT_TAG, * msgUnit, * "XmlScripting dump"); *</pre> * @param rootTag Usually XmlScriptInterpreter.ROOT_TAG="xmlBlaster" * @param msgUnit null is OK * @param comment Some comment you want to add or null * @return */ public static String wrapForScripting(String rootTag, MsgUnit msgUnit, String comment) { MsgUnit[] msgUnitArr = (msgUnit == null) ? new MsgUnit[0] : new MsgUnit[1]; if (msgUnitArr.length == 1) msgUnitArr[0] = msgUnit; return wrapForScripting(rootTag, msgUnitArr, comment); } public static String wrapForScripting(String rootTag, MsgUnit[] msgUnitArr, String comment) { if (msgUnitArr == null) msgUnitArr = new MsgUnit[0]; StringBuffer sb = new StringBuffer(1024+(msgUnitArr.length*256)); sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); sb.append("\n<").append(rootTag).append(">"); if (comment != null && comment.length() > 0) sb.append("\n<!-- " + comment + " -->"); for (int i=0; i<msgUnitArr.length; i++) { MsgUnit msgUnit = msgUnitArr[i]; String xml = msgUnit.toXml((String)null, (Properties)null); sb.append("\n <").append(msgUnit.getMethodName().toString()).append(">"); sb.append(xml); sb.append("\n </").append(msgUnit.getMethodName().toString()).append(">"); } sb.append("\n</").append(rootTag).append(">"); return sb.toString(); } /** * Dump the given MsgUnitRaw to XML. * Example for a PublishReturnQos: * <pre> * <-- A comment --> * <publish> <qos> <key oid='1'/> <rcvTimestamp nanos='1131654994574000000'/> <isPublish/> </qos> </publish></pre> * @param methodName The method to invoke, like "publishArr" * @param sessionId An optional sessionId or null * @param requestId An optional requestId or null * @param msgUnits The msgUnits to serialize * @param header For example <?xml version='1.0' encoding='UTF-8'?> * @param comment An optional comment to add, can be null * @param schemaDecl Used for root tag, for examplexmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:noNamespaceSchemaLocation='xmlBlasterPublish.xsd' * @param out The output sink for the result * @param type 'I' is for invoke, 'R' for reply and 'E' for exception * @throws XmlBlasterException */ protected void serialize(MethodName methodName, String sessionId, String requestId, MsgUnitRaw[] msgUnits, String header, String comment, String schemaDecl, OutputStream out, byte type) throws IOException { String offset = " "; StringBuffer sb = new StringBuffer(1024+(1024)); if (header == null) header = "<?xml version='1.0' encoding='UTF-8'?>\n"; sb.append(header); if (comment != null && comment.length() > 0) sb.append("\n<!-- ").append(comment).append( " -->"); if (methodName != null) { sb.append("\n<").append(methodName.toString()); if (schemaDecl != null && schemaDecl.length() > 0) sb.append(" ").append(schemaDecl); if (sessionId != null && sessionId.length() > 0) sb.append(" sessionId='").append(sessionId).append("'"); if (requestId != null && requestId.length() > 0) sb.append(" requestId='").append(requestId).append("'"); if (type != 0) { sb.append(" type='").append(MsgInfo.getTypeChar(type)).append("'"); } sb.append(">"); } out.write(sb.toString().getBytes()); if (msgUnits != null && msgUnits.length > 0) { if (msgUnits.length == 1) { if (type == MsgInfo.EXCEPTION_BYTE && this.sendSimpleExceptionFormat) { // This must be parsable on the other side similar to XmlBlasterException.parseByteArr() // See code in MsgInfo.java //ErrorCode errorCode = ErrorCode.getCategory(msgUnits[0].getQos()); -> toplevel only, like 'user' ErrorCode errorCode = ErrorCode.toErrorCode(msgUnits[0].getQos()); StringBuffer buf = new StringBuffer(1024); buf.append("<qos><state id='ERROR' info='").append(errorCode.getErrorCode()); buf.append("'/></qos>"); out.write(buf.toString().getBytes(Constants.UTF8_ENCODING)); } else { msgUnits[0].toXml(offset, out, (Properties)null); } } else { for (int i=0; i < msgUnits.length; i++) { out.write("\n <message>".getBytes()); msgUnits[i].toXml(offset, out, (Properties)null); out.write("\n </message>\n".getBytes()); } } } if (methodName != null) { out.write("\n</".getBytes()); out.write(methodName.toString().getBytes()); out.write(">\n".getBytes()); } out.flush(); } public static String dumpToFile(String path, String fn, String xml) throws XmlBlasterException { try { if (path != null) { File dir = new File(path); dir.mkdirs(); } File to_file = new File(path, fn); FileOutputStream to = new FileOutputStream(to_file); to.write(xml.getBytes()); to.close(); return to_file.toString(); } catch (java.io.IOException e) { throw new XmlBlasterException(Global.instance(), ErrorCode.USER_ILLEGALARGUMENT, "dumpToFile", "Please check your '"+path+"' or file name '" + fn + "'", e); } } /** * If a callback handler was registered, we will be notified here * about updates as well * @param cbSessionId * @param updateKey * @param content * @param updateQos * @return * @throws XmlBlasterException */ public String update(String cbSessionId, UpdateKey updateKey, byte[] content, UpdateQos updateQos) throws XmlBlasterException { synchronized (this.waitMutex) { if (updateQos.isOk()) this.updateCounter++; if (this.waitNumUpdates > 0 && this.updateCounter >= this.waitNumUpdates) { if (this.updateCounter == this.waitNumUpdates) log.info("Fire notify, " + this.updateCounter + " updates arrived"); waitMutex.notify(); } } if (log.isLoggable(Level.FINE)) log.fine("Received #" + this.updateCounter); return null; } public static void main(String[] args) { try { Global glob = new Global(); String[] tmp = new String[args.length-2]; for (int i=0; i < tmp.length; i++) tmp[i] = args[i+2]; glob.init(tmp); FileOutputStream out = new FileOutputStream(args[1]); XmlScriptInterpreter interpreter = new XmlScriptClient(glob, out); FileReader in = new FileReader(args[0]); interpreter.parse(in); } catch (Exception ex) { ex.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -