2 * Copyright (C) 2011 The Android Open Source Project
4 * Licensed under the Eclipse Public License, Version 1.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.eclipse.org/org/documents/epl-v10.php
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.android.ide.eclipse.adt.internal.editors.formatting;
18 import org.w3c.dom.Document;
19 import org.xml.sax.ErrorHandler;
20 import org.xml.sax.InputSource;
21 import org.xml.sax.SAXException;
22 import org.xml.sax.SAXParseException;
24 import java.io.StringReader;
26 import javax.xml.parsers.DocumentBuilder;
27 import javax.xml.parsers.DocumentBuilderFactory;
29 import junit.framework.TestCase;
31 public class XmlPrettyPrinterTest extends TestCase {
32 private void checkFormat(XmlFormatPreferences prefs, XmlFormatStyle style,
33 String xml, String expected, String delimiter) throws Exception {
35 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
36 InputSource is = new InputSource(new StringReader(xml));
37 factory.setIgnoringComments(false);
38 factory.setIgnoringElementContentWhitespace(false);
39 factory.setCoalescing(false);
40 factory.setNamespaceAware(true);
41 factory.setValidating(true);
42 DocumentBuilder builder = factory.newDocumentBuilder();
43 builder.setErrorHandler(new ErrorHandler() {
44 public void error(SAXParseException arg0) throws SAXException {
46 public void fatalError(SAXParseException arg0) throws SAXException {
48 public void warning(SAXParseException arg0) throws SAXException {
51 Document document = builder.parse(is);
53 XmlPrettyPrinter printer = new XmlPrettyPrinter(prefs, style, delimiter);
55 StringBuilder sb = new StringBuilder(1000);
56 printer.prettyPrint(-1, document, document, document, sb);
57 String formatted = sb.toString();
58 if (!expected.equals(formatted)) {
59 System.out.println(formatted);
61 assertEquals(expected, formatted);
64 private void checkFormat(XmlFormatPreferences prefs, XmlFormatStyle style,
65 String xml, String expected) throws Exception {
66 checkFormat(prefs, style, xml, expected, "\n"); //$NON-NLS-1$
68 private void checkFormat(XmlFormatStyle style, String xml, String expected)
70 XmlFormatPreferences prefs = XmlFormatPreferences.create();
71 checkFormat(prefs, style, xml, expected);
74 private void checkFormat(String xml, String expected) throws Exception {
75 checkFormat(XmlFormatStyle.LAYOUT, xml, expected);
78 public void testLayout1() throws Exception {
80 "<LinearLayout><Button></Button></LinearLayout>",
89 public void testLayout2() throws Exception {
91 "<LinearLayout><Button foo=\"bar\"></Button></LinearLayout>",
101 public void testLayout3() throws Exception {
102 XmlFormatPreferences prefs = XmlFormatPreferences.create();
103 prefs.oneAttributeOnFirstLine = true;
105 prefs, XmlFormatStyle.LAYOUT,
106 "<LinearLayout><Button foo=\"bar\"></Button></LinearLayout>",
109 " <Button foo=\"bar\">\n" +
116 // TODO: This test will only work with the Eclipse DOM because our parser doesn't
117 // handle the ElementImpl case which records empty elements
118 public void testClosedElements() throws Exception {
120 XmlPrettyPrinter.Style.RESOURCE,
122 "<item name=\"title_container\" type=\"id\" />\n" +
123 "<item name=\"title_logo\" type=\"id\"/>\n" +
126 " <item name=\"title_container\" type=\"id\" />\n" +
127 " <item name=\"title_logo\" type=\"id\" />\n" +
132 public void testResources() throws Exception {
134 XmlFormatStyle.RESOURCE,
135 "<resources><item name=\"foo\">Text value here </item></resources>",
137 " <item name=\"foo\">Text value here </item>\n" +
141 public void testNodeTypes() throws Exception {
142 // Ensures that a document with all kinds of node types is serialized correctly
144 XmlFormatStyle.LAYOUT,
145 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
148 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
151 "<!DOCTYPE metadata [\n" +
152 "<!ELEMENT metadata (category)*>\n" +
153 "<!ENTITY % ISOLat2\n" +
154 " SYSTEM \"http://www.xml.com/iso/isolat2-xml.entities\" >\n" +
157 " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
158 " android:orientation=\"vertical\">\n" +
160 "This is character data!\n" +
161 "<!-- This is not a comment! -->\n" +
162 "and <this is not an element>\n" +
164 "This is text: < and &\n" +
165 "<!-- comment 1 \"test\"... -->\n" +
166 "<!-- ... comment2 -->\n" +
169 "Type <key>less-than</key> (<)\n" +
173 /* For some reason the SAX document builder does not include the processing
174 * instruction node (the Eclipse XML parser luckily does; try to change
175 * unit test to use it since we want to make sure the pretty printer
176 * handles it properly)
177 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
181 " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
184 /* For some reason the SAX document builder does not include the
185 * doc type node (the Eclipse XML parser luckily does; try to change
186 * unit test to use it since we want to make sure the pretty printer
187 * handles it properly)
188 "<!DOCTYPE metadata [\n" +
189 "<!ELEMENT metadata (category)*>\n" +
190 "<!ENTITY % ISOLat2\n" +
191 " SYSTEM \"http://www.xml.com/iso/isolat2-xml.entities\" >\n" +
195 " xmlns:android=\"http://schemas.android.com/apk/res/android\"\n" +
196 " android:orientation=\"vertical\">\n" +
198 "This is character data!\n" +
199 "<!-- This is not a comment! -->\n" +
200 "and <this is not an element>\n" +
203 "This is text: < and &\n" +
205 " <!-- comment 1 \"test\"... -->\n" +
206 " <!-- ... comment2 -->\n" +
211 "Type <key>less-than</key> (<)\n" +
217 public void testWindowsDelimiters() throws Exception {
219 XmlFormatPreferences.create(), XmlFormatStyle.LAYOUT,
220 "<LinearLayout><Button foo=\"bar\"></Button></LinearLayout>",
221 "<LinearLayout>\r\n" +
224 " foo=\"bar\">\r\n" +