Author: sebb
Date: Sun Mar 11 10:29:06 2007
New Revision: 516963

URL: http://svn.apache.org/viewvc?view=rev&rev=516963
Log:
New XMLBuffer utility class
(currently only used by LDAP)

Added:
    
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/util/XMLBuffer.java
   (with props)
    
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jorphan/TestXMLBuffer.java  
 (with props)

Added: 
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/util/XMLBuffer.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/util/XMLBuffer.java?view=auto&rev=516963
==============================================================================
--- 
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/util/XMLBuffer.java
 (added)
+++ 
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/util/XMLBuffer.java
 Sun Mar 11 10:29:06 2007
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ */
+
+package org.apache.jorphan.util;
+
+import org.apache.commons.collections.ArrayStack;
+
+// @see org.apache.jorphan.util.TestXMLBuffer for unit tests
+
+/**
+ * Provides XML string building methods.
+ * Not synchronised.
+ *  
+ */
+public class XMLBuffer{
+       private StringBuffer sb = new StringBuffer(); // the string so far
+       
+       private ArrayStack tags = new ArrayStack(); // opened tags
+       
+       public XMLBuffer(){
+               
+       }
+       
+       private void startTag(String t){
+               sb.append("<");
+               sb.append(t);
+               sb.append(">");                 
+       }
+       private void endTag(String t){
+               sb.append("</");
+               sb.append(t);
+               sb.append(">");                 
+       }
+
+       /**
+        * Open a tag; save on stack.
+        * 
+        * @param tagname
+        * @return this
+        */
+       public XMLBuffer openTag(String tagname){
+               tags.push(tagname);
+               startTag(tagname);
+               return this;
+       }
+       
+       /**
+        * Close top tag from stack.
+        * 
+        * @param tagname
+        * 
+        * @return this
+        * 
+        * @throws IllegalArgumentException if the tag names do not match
+        */
+       public XMLBuffer closeTag(String tagname){
+               String tag = (String) tags.pop();
+               if (!tag.equals(tagname)) {
+                       throw new IllegalArgumentException("Trying to close 
tag: "+tagname+" ; should be "+tag);
+               }
+               endTag(tag);
+               return this;
+       }
+       
+       /**
+        * Add a complete tag with content.
+        * 
+        * @param tagname
+        * @param content
+        * @return
+        */
+       public XMLBuffer tag(String tagname,String content){
+               startTag(tagname);
+               sb.append(content);
+               endTag(tagname);
+               return this;
+       }
+
+       /**
+        * Convert the buffer to a string, closing any open tags
+        */
+       public String toString(){
+               while(!tags.isEmpty()){
+                       endTag((String)tags.pop());
+               }                       
+               return sb.toString();
+       }
+}
\ No newline at end of file

Propchange: 
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/util/XMLBuffer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/jmeter/branches/rel-2-2/src/jorphan/org/apache/jorphan/util/XMLBuffer.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Added: 
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jorphan/TestXMLBuffer.java
URL: 
http://svn.apache.org/viewvc/jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jorphan/TestXMLBuffer.java?view=auto&rev=516963
==============================================================================
--- 
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jorphan/TestXMLBuffer.java 
(added)
+++ 
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jorphan/TestXMLBuffer.java 
Sun Mar 11 10:29:06 2007
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * 
+ */
+
+package org.apache.jorphan;
+
+import org.apache.jmeter.junit.JMeterTestCase;
+import org.apache.jorphan.util.XMLBuffer;
+
+public class TestXMLBuffer extends JMeterTestCase {
+
+       public TestXMLBuffer(String arg0) {
+               super(arg0);
+       }
+    
+       public void test1() throws Exception{
+               XMLBuffer xb = new XMLBuffer();
+               xb.openTag("start");
+               assertEquals("<start></start>",xb.toString());
+       }
+       
+       public void test2() throws Exception{
+               XMLBuffer xb = new XMLBuffer();
+               xb.tag("start","now");
+               assertEquals("<start>now</start>",xb.toString());
+       }
+       public void test3() throws Exception{
+               XMLBuffer xb = new XMLBuffer();
+               xb.openTag("abc");
+               xb.closeTag("abc");
+               assertEquals("<abc></abc>",xb.toString());
+       }
+       public void test4() throws Exception{
+               XMLBuffer xb = new XMLBuffer();
+               xb.openTag("abc");
+               try {
+                       xb.closeTag("abcd");
+                       fail("Should have caused IllegalArgumentException");
+               } catch (IllegalArgumentException e) {
+               }
+       }
+}

Propchange: 
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jorphan/TestXMLBuffer.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
jakarta/jmeter/branches/rel-2-2/test/src/org/apache/jorphan/TestXMLBuffer.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to