OSDN Git Service

[added] XmlBuilder.
authornathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Sat, 6 Aug 2011 08:48:28 +0000 (08:48 +0000)
committernathan.sweet <nathan.sweet@6c4fd544-2939-11df-bb46-9574ba5d0bfa>
Sat, 6 Aug 2011 08:48:28 +0000 (08:48 +0000)
gdx/src/com/badlogic/gdx/utils/XmlBuilder.java [new file with mode: 0644]

diff --git a/gdx/src/com/badlogic/gdx/utils/XmlBuilder.java b/gdx/src/com/badlogic/gdx/utils/XmlBuilder.java
new file mode 100644 (file)
index 0000000..c6536af
--- /dev/null
@@ -0,0 +1,114 @@
+/*******************************************************************************\r
+ * Copyright 2011 See AUTHORS file.\r
+ * \r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ * \r
+ *   http://www.apache.org/licenses/LICENSE-2.0\r
+ * \r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ ******************************************************************************/\r
+\r
+package com.badlogic.gdx.utils;\r
+\r
+import java.io.IOException;\r
+import java.io.Writer;\r
+import java.util.ArrayDeque;\r
+\r
+//@off\r
+/**\r
+ * Convenience class for emitting XML. <pre>\r
+ * StringWriter writer = new StringWriter();\r
+ * XmlBuilder xml = new XmlBuilder(writer);\r
+ * xml.element("meow")\r
+ *     .attribute("moo", "cow")\r
+ *     .element("child")\r
+ *             .attribute("moo", "cow")\r
+ *             .element("child")\r
+ *                     .attribute("moo", "cow")\r
+ *                     .text("XML is like violence. If it doesn't solve your problem, you're not using enough of it.")\r
+ *             .pop()\r
+ *     .pop()\r
+ * .pop();\r
+ * System.out.println(writer);\r
+ * </pre>\r
+ * @author Nathan Sweet\r
+ */\r
+//@on\r
+public class XmlBuilder {\r
+       private final Writer writer;\r
+       private final ArrayDeque<String> stack = new ArrayDeque();\r
+       private String currentElement;\r
+\r
+       public XmlBuilder (Writer writer) {\r
+               this.writer = writer;\r
+       }\r
+\r
+       private void indent () throws IOException {\r
+               int count = stack.size();\r
+               if (currentElement != null) count++;\r
+               for (int i = 0; i < count; i++)\r
+                       writer.write('\t');\r
+       }\r
+\r
+       public XmlBuilder element (String name) throws IOException {\r
+               startElementContent();\r
+               writer.write('<');\r
+               writer.write(name);\r
+               currentElement = name;\r
+               return this;\r
+       }\r
+\r
+       private void startElementContent () throws IOException {\r
+               if (currentElement != null) {\r
+                       stack.push(currentElement);\r
+                       currentElement = null;\r
+                       writer.write(">\n");\r
+               }\r
+               indent();\r
+       }\r
+\r
+       public XmlBuilder attribute (String name, String value) throws IOException {\r
+               if (currentElement == null) throw new IllegalStateException();\r
+               writer.write(' ');\r
+               writer.write(name);\r
+               writer.write("=\"");\r
+               writer.write(value);\r
+               writer.write('"');\r
+               return this;\r
+       }\r
+\r
+       public XmlBuilder text (String text) throws IOException {\r
+               startElementContent();\r
+               writer.write(text);\r
+               writer.write('\n');\r
+               return this;\r
+       }\r
+\r
+       public XmlBuilder pop () throws IOException {\r
+               if (currentElement != null) {\r
+                       writer.write("/>\n");\r
+                       currentElement = null;\r
+               } else {\r
+                       String name = stack.pop();\r
+                       indent();\r
+                       writer.write("</");\r
+                       writer.write(name);\r
+                       writer.write(">\n");\r
+               }\r
+               return this;\r
+       }\r
+\r
+       /**\r
+        * Calls {@link #pop()} for each remaining open element, if any.\r
+        */\r
+       public void close () throws IOException {\r
+               while (!stack.isEmpty())\r
+                       pop();\r
+       }\r
+}\r