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

📄 contextnode.java

📁 java开源的企业总线.xmlBlaster
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      return (ContextNode[])list.toArray(new ContextNode[list.size()]);   }      /**    * Remove a child.     * @param child    * @return true if the child was removed    */   public synchronized boolean removeChild(ContextNode child) {      if (this.childs == null || this.childs.size() == 0 || child == null) {         return false;      }      Object removed = this.childs.remove(child.getAbsoluteName());      return (removed != null);   }   /**    * Search down the children tree for the given className and instanceName.     * Only the first match is returned    * @param className If null only a given instanceName is searched    * @param instanceName If null only the given className is searched    * @return The child, is null if not found    */   public ContextNode getChild(String className, String instanceName) {      return getChild(this, className, instanceName);   }   /**    * Search down the children tree for the given className.     * Only the first match is returned    * @param className If null only a given instanceName is searched    * @return The child, is null if not found    */   public ContextNode getChild(String className) {      return getChild(this, className, null);   }   /**    * Recursive search    */   protected ContextNode getChild(ContextNode node, String className, String instanceName) {      if (className == null && instanceName == null) return null;      if (className == null && node.getInstanceNameNotNull().equals(instanceName))         return node;      if (instanceName == null && node.getClassName().equals(className))         return node;      if (node.getClassName().equals(className) && node.getInstanceNameNotNull().equals(instanceName))         return node;      ContextNode[] childsArr = node.getChildren();      for (int i=0; i<childsArr.length; i++) {         return getChild(childsArr[i], className, instanceName);      }      return null;   }   /**    * Access the absolute name in standard notation    * @return e.g. "/xmlBlaster/node/heron/client/joe/session/2", never null    */   public String getAbsoluteName() {      StringBuffer sb = new StringBuffer(256);      if (this.parent == ROOT_NODE) {         sb.append(ROOT_MARKER_TAG).append(SEP).append(this.className);         if (this.instanceName != null) {            sb.append(SEP).append(this.instanceName);         }         return sb.toString();      }      sb.append(this.parent.getAbsoluteName()).append(SEP).append(this.className);      if (this.instanceName != null) {         sb.append(SEP).append(this.instanceName);      }      return sb.toString();   }   /**    * Access the absolute name in standard notation    * @param schema Currently only "xpath"    * @return e.g. "xpath:/xmlBlaster/node[@id='heron']/client[@id='joe']/session[@id='2']", never null    */   public String getAbsoluteName(String schema) {      StringBuffer sb = new StringBuffer(256);      if (SCHEMA_JMX.equalsIgnoreCase(schema)) {         // "org.xmlBlaster:nodeClass=node,node=heron,clientClass=client,client=joe,queueClass=queue,queue=subject665,entryClass=entry,entry=1002"         // like this jconsole creates a nice tree (see JmxWrapper.java for a discussion)         if (this.parent == ROOT_NODE) {            sb.append(SCHEMA_JMX_DOMAIN).append(":").append("nodeClass=node,node=");            sb.append((this.instanceName==null)?"null":ObjectName.quote(this.instanceName));            return sb.toString();         }         sb.append(this.parent.getAbsoluteName(schema));         sb.append(",").append(this.className).append("Class=").append(this.className);         // JMX ',' make problems with or without quotes         //     ':' is only OK if quoted         if (this.instanceName != null) {            sb.append(",").append(this.className).append("=").append(ObjectName.quote(this.instanceName));         }         return sb.toString();      }      else if (SCHEMA_XPATH.equalsIgnoreCase(schema)) {         if (this.parent == ROOT_NODE) {            sb.append(schema).append(":").append(ROOT_MARKER_TAG).append(SEP).append(this.className);            if (this.instanceName != null) {               sb.append("[@id='").append(this.instanceName).append("']");            }            return sb.toString();         }         sb.append(this.parent.getAbsoluteName(schema)).append(SEP).append(this.className);         if (this.instanceName != null) {            sb.append("[@id='").append(this.instanceName).append("']");         }         return sb.toString();      }      else /* if (SCHEMA_URL.equalsIgnoreCase(schema)) */ {         return getAbsoluteName();      }   }   /**    * @return #getAbsoluteName()    */   public String toString() {      return getAbsoluteName();   }   /**    * @return e.g. "client/joe", never null    */   public String getRelativeName() {      StringBuffer sb = new StringBuffer(256);      sb.append(this.className);      if (this.instanceName != null) {         sb.append(SEP).append(this.instanceName);      }      return sb.toString();   }   /**    * @param schema Currently only "xpath"    * @return e.g. "xpath:client[@id='joe']", never null    */   public String getRelativeName(String schema) {      StringBuffer sb = new StringBuffer(256);      if (SCHEMA_JMX.equalsIgnoreCase(schema)) {         // "org.xmlBlaster:clientClass=client,client=joe"         sb.append(SCHEMA_JMX_DOMAIN).append(":");         sb.append(this.className).append("Class=").append(this.className);         if (this.instanceName != null) {            sb.append(",").append(this.className).append("=").append(ObjectName.quote(this.instanceName));         }         return sb.toString();      }      else if (SCHEMA_XPATH.equalsIgnoreCase(schema)) {         sb.append(schema).append(":").append(this.className);         if (this.instanceName != null) {            sb.append("[@id='").append(this.instanceName).append("']");         }         return sb.toString();      }      else /* if (SCHEMA_URL.equalsIgnoreCase(schema)) */ {         return getRelativeName();      }   }   /**    * @return true if relative name equals    */   public boolean equalsRelative(ContextNode contextNode) {      return getRelativeName().equals(contextNode.getRelativeName());   }   /**    * Compare the absolute name.     * @param contextNode Returns false if null    * @return    */   public boolean equalsAbsolute(ContextNode contextNode) {      if (contextNode == null) return false;      return getAbsoluteName().equals(contextNode.getAbsoluteName());   }   /**    * Parse the given string.    * NOTE: The returned ContextNode is a new instance and NOT the instance from inside a given Global     * @param url For example     *             "/xmlBlaster/node/heron/client/joe/session/2"    * @return The lowest ContextNode instance, you can navigate upwards with getParent()    *         or ContextNode.ROOT_NODE==null.    */   public static ContextNode valueOf(String url) {      if (url == null || url.length() == 0)         return ROOT_NODE;      String lower = url.toLowerCase();      if (lower.startsWith("xpath")) {         throw new IllegalArgumentException("ContextNode.valueOf(): Unkown schema in '" + url + "'");      }      if (url.startsWith(SCHEMA_JMX_DOMAIN+":")) { // SCHEMA_JMX         // org.xmlBlaster:nodeClass=node,node="heron",clientClass=connection,connection="jack",queueClass=queue,queue="connection-99"         int index = url.indexOf(":");         String tmp = url.substring(index+1);         String[] toks = StringPairTokenizer.parseLine(tmp, ',', QUOTE, true);         ContextNode node = ROOT_NODE;         for (int i=0; i<toks.length; i++) {            index = toks[i].indexOf("=");            String className = (index > 0) ? toks[i].substring(index+1) : null;            if (className.startsWith("\""))                className = className.substring(1,className.length()-1);            String instanceName = null;                if (i < toks.length-1) {               index = toks[i+1].indexOf("=");               instanceName = (index > 0) ? toks[i+1].substring(index+1) : null;               if (instanceName.startsWith("\""))                instanceName = instanceName.substring(1,instanceName.length()-1);               i++;                }            node = new ContextNode(className, instanceName, node);         }         return node;      }      else if (url.startsWith("/xmlBlaster/node/") || url.startsWith("/node/") || !url.startsWith("/")/*relative name*/) {         String[] toks = StringPairTokenizer.parseLine(url, '/', QUOTE, true);         ContextNode node = ROOT_NODE;         for (int i=0; i<toks.length; i++) {            String className = toks[i];            String instanceValue = null;            if (i == 0 && "xmlBlaster".equals(className)) {               node = ROOT_NODE;               continue;            }            if (i == toks.length-1) {               if (node != null && ContextNode.SUBJECT_MARKER_TAG.equals(node.getClassName())) {                   className = ContextNode.SESSION_MARKER_TAG;                   i--; // Hack: We add "session" for "client/joe/1" -> "client/joe/session/1" for backward compatibility                   instanceValue = toks[i+1];               }               else {                  // For example "/xmlBlaster/node/heron/logging"                  instanceValue = null;                  log.fine("Unexpected syntax in '" + url + "', missing value for class, we assume a 'null' value.");               }            }            else {               instanceValue = toks[i+1];            }            node = new ContextNode(className, instanceValue, node);            i++;         }         return node;      }            throw new IllegalArgumentException("ContextNode.valueOf(): unknown formatting schema is not implemented: '" + url + "'");   }   /**    * Dump state of this object into XML.    * <br>    * @return XML dump of ContextNode    */   public final String toXml() {      return toXml((String)null);   }   /**    * Dump state of this object into XML.    * <br>    * @param extraOffset indenting of tags    * @return XML dump of ContextNode    */   public final String toXml(String extraOffset) {      StringBuffer sb = new StringBuffer(256);      if (extraOffset == null) extraOffset = "";      String offset = Constants.OFFSET + extraOffset;      sb.append(offset).append("<ContextNode class='").append(this.className).append("' instance='").append(this.instanceName).append("'/>");      return sb.toString();   }   /**    * Method for testing only.<p />    *    * Invoke: java -Djava.compiler= org.xmlBlaster.util.context.ContextNode    */   public static void main(String args[]) {      try {         ContextNode heron = new ContextNode(ContextNode.CLUSTER_MARKER_TAG, "heron", null);         System.out.println("AbsoluteName=" + heron.getAbsoluteName() + " RelativeName=" + heron.getRelativeName());         ContextNode jack = new ContextNode(ContextNode.SUBJECT_MARKER_TAG, "jack", heron);         System.out.println("AbsoluteName=" + jack.getAbsoluteName() + " RelativeName=" + jack.getRelativeName());                  ContextNode ses2 = new ContextNode(ContextNode.SESSION_MARKER_TAG, "2", jack);         System.out.println("AbsoluteName=" + ses2.getAbsoluteName() + " RelativeName=" + ses2.getRelativeName());         System.out.println("AbsoluteName=" + ses2.getAbsoluteName("xpath") + " RelativeName=" + ses2.getRelativeName("xpath"));         System.out.println("AbsoluteName=" + ses2.getAbsoluteName("jmx") + " RelativeName=" + ses2.getRelativeName("jmx"));         {            System.out.println("\nTopic:");            ContextNode hello = new ContextNode(ContextNode.TOPIC_MARKER_TAG, "hello", heron);            System.out.println("AbsoluteName=" + hello.getAbsoluteName() + " RelativeName=" + hello.getRelativeName());            System.out.println("AbsoluteName=" + hello.getAbsoluteName("xpath") + " RelativeName=" + hello.getRelativeName("xpath"));            System.out.println("AbsoluteName=" + hello.getAbsoluteName("jmx") + " RelativeName=" + hello.getRelativeName("jmx"));         }         {            System.out.println("\nWith NULL:");            ContextNode hello = new ContextNode(ContextNode.TOPIC_MARKER_TAG, null, heron);            System.out.println("AbsoluteName=" + hello.getAbsoluteName() + " RelativeName=" + hello.getRelativeName());            System.out.println("AbsoluteName=" + hello.getAbsoluteName("xpath") + " RelativeName=" + hello.getRelativeName("xpath"));            System.out.println("AbsoluteName=" + hello.getAbsoluteName("jmx") + " RelativeName=" + hello.getRelativeName("jmx"));         }         {            System.out.println("\nMERGE:");            ContextNode root = ContextNode.valueOf("/node/heron/client/joe");            ContextNode other = ContextNode.valueOf("/node/xyz/client/joe/session/1");            ContextNode leaf = root.mergeChildTree(other);            // -> /xmlBlaster/node/heron/client/joe/session/1            System.out.println("Orig=" + root.getAbsoluteName() + " merge=" + other.getAbsoluteName() + " result=" + leaf.getAbsoluteName());         }         {            System.out.println("\nMERGE:");            ContextNode root = ContextNode.valueOf("/node/heron/client/joe/session/1");            ContextNode other = ContextNode.valueOf("/node/xyz/service/Pop3Driver");            ContextNode leaf = root.mergeChildTree(other);            // -> /xmlBlaster/node/heron/client/joe/session/1/service/Pop3Driver            System.out.println("Orig=" + root.getAbsoluteName() + " merge=" + other.getAbsoluteName() + " result=" + ((leaf==null)?"null":leaf.getAbsoluteName()) );         }         {            System.out.println("\nMERGE:");            ContextNode root = ContextNode.valueOf("/node/heron/client/joe/session/1");            ContextNode other = ContextNode.valueOf("/node/clientjoe1/\"connection:client/joe/1\"");            ContextNode leaf = root.mergeChildTree(other);            // -> /xmlBlaster/node/heron/client/joe/session/1/service/Pop3Driver            System.out.println("Orig=" + root.getAbsoluteName() + " merge=" + other.getAbsoluteName() + " result=" + ((leaf==null)?"null":leaf.getAbsoluteName()) );         }      }      catch (IllegalArgumentException e) {         System.out.println("ERROR: " + e.toString());      }   }}

⌨️ 快捷键说明

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