Skip to main content
summaryrefslogtreecommitdiffstats
blob: ae1a44687439b24b010b8e112964a7fc64e3d2f3 (plain) (blame)
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
package org.eclipse.team.internal.ccvs.core.util;

/*
 * (c) Copyright IBM Corp. 2000, 2001.
 * All Rights Reserved.
 */
import java.util.*;
import org.eclipse.core.resources.*;
import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.team.ccvs.core.CVSProviderPlugin;

import java.io.*;

// NIK: Maybe we should make the Strings constants ?

public class ProjectDescriptionWriter {
	private static void appendEscapedChar(StringBuffer buffer, char c) {
		String replacement = getReplacement(c);
		if (replacement != null) {
			buffer.append('&');
			buffer.append(replacement);
			buffer.append(';');
		} else {
			if ((c >= ' ' && c <= 0x7E) || c == '\n' || c == '\r' || c == '\t') {
				buffer.append(c);
			} else {
				buffer.append("&#"); //$NON-NLS-1$
				buffer.append(Integer.toString(c));
				buffer.append(';');
			}
		}
	}
	public static String getEscaped(String s) {
		StringBuffer result = new StringBuffer(s.length() + 10);
		for (int i = 0; i < s.length(); ++i)
			appendEscapedChar(result, s.charAt(i));
		return result.toString();
	}
	private static String getReplacement(char c) {
		// Encode special XML characters into the equivalent character references.
		// These five are defined by default for all XML documents.
		switch (c) {
			case '<' :
				return "lt"; //$NON-NLS-1$
			case '>' :
				return "gt"; //$NON-NLS-1$
			case '"' :
				return "quot"; //$NON-NLS-1$
			case '\'' :
				return "apos"; //$NON-NLS-1$
			case '&' :
				return "amp"; //$NON-NLS-1$
		}
		return null;
	}
	public static void writeProjectDescription(
		IProjectDescription desc,
		OutputStream os)
		throws IOException {
		PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF8")); //$NON-NLS-1$
		writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); //$NON-NLS-1$
		writer.println("<project-description>"); //$NON-NLS-1$

		String comment = desc.getComment();
		if (comment != null) {
			writer.print("\t<comment>"); //$NON-NLS-1$
			writer.print(getEscaped(desc.getComment()));
			writer.println("</comment>"); //$NON-NLS-1$
		}

		String[] natures = desc.getNatureIds();
		for (int i = 0; i < natures.length; i++) {
			if ( ! natures[i].equals(CVSProviderPlugin.getTypeId()))
				writer.println("\t<nature id=\"" + getEscaped(natures[i]) + "\"/>"); //$NON-NLS-1$  //$NON-NLS-2$
		}

		IProject[] references = desc.getReferencedProjects();
		for (int i = 0; i < references.length; i++) {
			writer.println(
				"\t<reference project-name=\"" + getEscaped(references[i].getName()) + "\"/>"); //$NON-NLS-1$  //$NON-NLS-2$
		}

		ICommand[] commands = desc.getBuildSpec();
		for (int i = 0; i < commands.length; i++) {
			writer.println(
				"\t<builder name=\"" + getEscaped(commands[i].getBuilderName()) + "\">"); //$NON-NLS-1$ //$NON-NLS-2$
			Map args = commands[i].getArguments();
			for (Iterator it = args.keySet().iterator(); it.hasNext();) {
				String argName = (String) it.next();
				String argValue = (String) args.get(argName);
				writer.println(
					"\t\t<arg name=\"" //$NON-NLS-1$
						+ getEscaped(argName)
						+ "\" value=\"" //$NON-NLS-1$
						+ getEscaped(argValue)
						+ "\"/>"); //$NON-NLS-1$
			}
			writer.println("\t</builder>"); //$NON-NLS-1$
		}

		writer.println("</project-description>"); //$NON-NLS-1$
		writer.flush();
	}
}

Back to the top