587
|
1 |
/*
|
|
2 |
* Copyright (c) 2007-2008 Nokia Corporation and/or its subsidiary(-ies).
|
|
3 |
* All rights reserved.
|
|
4 |
* This component and the accompanying materials are made available
|
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
|
6 |
* which accompanies this distribution, and is available
|
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
|
8 |
*
|
|
9 |
* Initial Contributors:
|
|
10 |
* Nokia Corporation - initial contribution.
|
|
11 |
*
|
|
12 |
* Contributors:
|
|
13 |
*
|
|
14 |
* Description:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
package com.nokia.helium.core.ant.filters;
|
|
19 |
|
628
|
20 |
import java.io.IOException;
|
587
|
21 |
import java.io.StringWriter;
|
|
22 |
|
|
23 |
import org.apache.tools.ant.BuildException;
|
|
24 |
import org.apache.tools.ant.filters.TokenFilter.Filter;
|
|
25 |
import org.dom4j.Document;
|
|
26 |
import org.dom4j.DocumentHelper;
|
|
27 |
import org.dom4j.io.OutputFormat;
|
|
28 |
import org.dom4j.io.XMLWriter;
|
|
29 |
|
|
30 |
/**
|
|
31 |
* Prints xml file in pretty format.
|
|
32 |
*
|
|
33 |
*/
|
|
34 |
public class PrettyPrintXmlFilter implements Filter {
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Filter the input string.
|
|
38 |
*
|
|
39 |
* @param string
|
|
40 |
* the string to filter
|
|
41 |
* @return the modified string
|
|
42 |
*/
|
|
43 |
public String filter(String token) {
|
|
44 |
String output = token;
|
|
45 |
XMLWriter writer = null;
|
|
46 |
if (token.length() > 0) {
|
|
47 |
try {
|
|
48 |
Document doc = DocumentHelper.parseText(token);
|
|
49 |
StringWriter out = new StringWriter();
|
|
50 |
OutputFormat format = OutputFormat.createPrettyPrint();
|
|
51 |
format.setIndentSize(4);
|
|
52 |
writer = new XMLWriter(out, format);
|
|
53 |
writer.write(doc);
|
|
54 |
|
|
55 |
output = out.toString();
|
|
56 |
} catch (org.dom4j.DocumentException exc) {
|
|
57 |
throw new BuildException(exc.getMessage(), exc);
|
|
58 |
} catch (IOException exc) {
|
|
59 |
throw new BuildException(exc.getMessage(), exc);
|
|
60 |
} finally {
|
|
61 |
try {
|
628
|
62 |
if (writer != null) {
|
587
|
63 |
writer.close();
|
628
|
64 |
}
|
587
|
65 |
} catch (IOException exc) {
|
|
66 |
throw new BuildException(exc.getMessage(), exc);
|
|
67 |
}
|
|
68 |
}
|
|
69 |
}
|
|
70 |
return output;
|
|
71 |
}
|
|
72 |
}
|