mnaufaldillah

XMLBuffer Jmeter

Oct 23rd, 2021
567
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to you under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  * http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17.  
  18. package org.apache.jorphan.util;
  19.  
  20. import java.util.ArrayDeque;
  21.  
  22. // @see org.apache.jorphan.util.TestXMLBuffer for unit tests
  23.  
  24. /**
  25.  * Provides XML string building methods.
  26.  * Not synchronised.
  27.  *
  28.  */
  29. public class XMLBuffer{
  30.     private final StringBuilder sb = new StringBuilder(); // the string so far
  31.  
  32.     private final ArrayDeque<String> tags = new ArrayDeque<>(); // opened tags
  33.  
  34.     public XMLBuffer() {
  35.  
  36.     }
  37.  
  38.     private void startTag(String t) {
  39.         sb.append("<");
  40.         sb.append(t);
  41.         sb.append(">");
  42.     }
  43.  
  44.     private void endTag(String t) {
  45.         sb.append("</");
  46.         sb.append(t);
  47.         sb.append(">");
  48.         sb.append("\n");
  49.     }
  50.  
  51.     private void emptyTag(String t) {
  52.         sb.append("<");
  53.         sb.append(t);
  54.         sb.append("/>");
  55.         sb.append("\n");
  56.     }
  57.  
  58.     /**
  59.      * Open a tag; save on stack.
  60.      *
  61.      * @param tagName name of the tag
  62.      * @return this
  63.      */
  64.     public XMLBuffer openTag(String tagName) {
  65.         tags.push(tagName);
  66.         startTag(tagName);
  67.         return this;
  68.     }
  69.  
  70.     /**
  71.      * Close top tag from stack.
  72.      *
  73.      * @param tagName name of the tag to close
  74.      *
  75.      * @return this
  76.      *
  77.      * @throws IllegalArgumentException if the tag names do not match
  78.      */
  79.     public XMLBuffer closeTag(String tagName) {
  80.         String tag = tags.pop();
  81.         if (!tag.equals(tagName)) {
  82.             throw new IllegalArgumentException(
  83.                     "Trying to close tag: " + tagName + " ; should be " + tag);
  84.         }
  85.         endTag(tag);
  86.         return this;
  87.     }
  88.  
  89.     /**
  90.      * Add a complete tag with content.
  91.      *
  92.      * @param tagName name of the tag
  93.      * @param content content to put in tag, or empty content, if an empty tag should be used
  94.      * @return this
  95.      */
  96.     public XMLBuffer tag(String tagName, CharSequence content) {
  97.         if (content.length() == 0) {
  98.             emptyTag(tagName);
  99.         } else {
  100.             startTag(tagName);
  101.             sb.append(content);
  102.             endTag(tagName);
  103.         }
  104.         return this;
  105.     }
  106.  
  107.     /**
  108.      * Convert the buffer to a string, closing any open tags
  109.      */
  110.     @Override
  111.     public String toString() {
  112.         while (!tags.isEmpty()) {
  113.             endTag(tags.pop());
  114.         }
  115.         return sb.toString();
  116.     }
  117. }
RAW Paste Data