File: XmlUtils.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (109 lines) | stat: -rw-r--r-- 2,979 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package tim.prune.save.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

/**
 * Collection of utility functions for handling XML
 */
public abstract class XmlUtils
{
	/** Start of Cdata sequence */
	private static final String CDATA_START = "<![CDATA[";
	/** End of Cdata sequence */
	private static final String CDATA_END = "]]>";
	/** Cached copy of system encoding string */
	private static String _systemEncoding = null;

	/**
	 * Fix the CDATA blocks in the given String to give valid xml
	 * @param inString String to modify
	 * @return fixed String
	 */
	public static String fixCdata(String inString)
	{
		if (inString == null) return "";
		if (inString.indexOf('<') < 0 && inString.indexOf('>') < 0) {
			return inString;
		}
		String result = inString;
		// Remove cdata block at start if present
		if (result.startsWith(CDATA_START)) {
			result = result.substring(CDATA_START.length());
		}
		// Remove all instances of end block
		result = result.replaceAll(CDATA_END, "");
		// Now check whether cdata block is required
		if (result.indexOf('<') < 0 && result.indexOf('>') < 0) {
			return result;
		}
		return CDATA_START + result + CDATA_END;
	}


	/**
	 * @return true if system uses UTF-8 by default
	 */
	public static boolean isSystemUtf8()
	{
		String systemEncoding = getSystemEncoding();
		return (systemEncoding != null && systemEncoding.toUpperCase().equals("UTF-8"));
	}

	/**
	 * @return name of the system's character encoding
	 */
	public static String getSystemEncoding()
	{
		if (_systemEncoding == null) {
			_systemEncoding = determineSystemEncoding();
		}
		return _systemEncoding;
	}

	/**
	 * Use a temporary file to obtain the name of the default system encoding
	 * @return name of default system encoding, or null if write failed
	 */
	private static String determineSystemEncoding()
	{
		File tempFile = null;
		String encoding = null;
		try
		{
			tempFile = File.createTempFile("gpsprune", null);
			OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempFile));
			encoding = getEncoding(writer);
			writer.close();
		}
		catch (IOException e) {} // value stays null
		// Delete temp file
		if (tempFile != null && tempFile.exists()) {
			if (!tempFile.delete()) {
				System.err.println("Cannot delete temp file: " + tempFile.getAbsolutePath());
			}
		}
		// If writing failed (eg permissions) then just ask system for default
		if (encoding == null) encoding = Charset.defaultCharset().name();
		return encoding;
	}


	/**
	 * Get the default system encoding using a writer
	 * @param inWriter writer object
	 * @return string defining encoding
	 */
	public static String getEncoding(OutputStreamWriter inWriter)
	{
		String encoding = inWriter.getEncoding();
		try {
			encoding =  Charset.forName(encoding).name();
		}
		catch (Exception e) {} // ignore failure to find encoding
		return encoding;
	}
}