Lines 14-20
Link Here
|
14 |
import java.io.*; |
14 |
import java.io.*; |
15 |
import java.util.*; |
15 |
import java.util.*; |
16 |
import javax.xml.parsers.ParserConfigurationException; |
16 |
import javax.xml.parsers.ParserConfigurationException; |
17 |
import org.eclipse.core.internal.content.*; |
17 |
import org.eclipse.core.internal.content.ContentMessages; |
|
|
18 |
import org.eclipse.core.internal.content.XMLRootHandler; |
18 |
import org.eclipse.core.internal.runtime.RuntimeLog; |
19 |
import org.eclipse.core.internal.runtime.RuntimeLog; |
19 |
import org.eclipse.core.runtime.*; |
20 |
import org.eclipse.core.runtime.*; |
20 |
import org.eclipse.osgi.util.NLS; |
21 |
import org.eclipse.osgi.util.NLS; |
Lines 65-72
Link Here
|
65 |
* @noinstantiate This class is not intended to be instantiated by clients. |
66 |
* @noinstantiate This class is not intended to be instantiated by clients. |
66 |
*/ |
67 |
*/ |
67 |
public final class XMLRootElementContentDescriber2 extends XMLContentDescriber implements IExecutableExtension { |
68 |
public final class XMLRootElementContentDescriber2 extends XMLContentDescriber implements IExecutableExtension { |
|
|
69 |
|
70 |
static final String DTD = "org.eclipse.core.runtime.content.XMLRootElementContentDescriber2.dtd"; //$NON-NLS-1$ |
71 |
static final String NAMESPACE = "org.eclipse.core.runtime.content.XMLRootElementContentDescriber2.namespace"; //$NON-NLS-1$ |
72 |
static final String ELEMENT = "org.eclipse.core.runtime.content.XMLRootElementContentDescriber2.element"; //$NON-NLS-1$ |
73 |
static final String RESULT = "org.eclipse.core.runtime.content.XMLRootElementContentDescriber2.result"; //$NON-NLS-1$ |
74 |
|
68 |
private static final String ELEMENT_TO_FIND = "element"; //$NON-NLS-1$ |
75 |
private static final String ELEMENT_TO_FIND = "element"; //$NON-NLS-1$ |
69 |
|
76 |
|
70 |
/* (Intentionally not included in javadoc) |
77 |
/* (Intentionally not included in javadoc) |
71 |
* The top-level elements we are looking for. This value will be initialized |
78 |
* The top-level elements we are looking for. This value will be initialized |
72 |
* by the <code>setInitializationData</code> method. If no value is |
79 |
* by the <code>setInitializationData</code> method. If no value is |
Lines 78-100
Link Here
|
78 |
/* (Intentionally not included in javadoc) |
85 |
/* (Intentionally not included in javadoc) |
79 |
* Simple value holder for root element name, its namespace and dtd. |
86 |
* Simple value holder for root element name, its namespace and dtd. |
80 |
*/ |
87 |
*/ |
81 |
private class QualifiedElement { |
88 |
private class QualifiedElement { |
82 |
private String namespace; |
89 |
private String namespace; |
83 |
private String element; |
90 |
private String element; |
84 |
private String dtd; |
91 |
private String dtd; |
85 |
|
92 |
|
86 |
public QualifiedElement(String qualifiedElement) { |
93 |
public QualifiedElement(String qualifiedElement) { |
87 |
// Extract namespace part |
94 |
// Extract namespace part |
88 |
int openBrace = qualifiedElement.indexOf('{'); |
95 |
int openBrace = qualifiedElement.indexOf('{'); |
89 |
int closeBrace = qualifiedElement.indexOf('}'); |
96 |
int closeBrace = qualifiedElement.indexOf('}'); |
90 |
if (openBrace == 0 && closeBrace >=1 ) { |
97 |
if (openBrace == 0 && closeBrace >= 1) { |
91 |
namespace = qualifiedElement.substring(1, closeBrace); |
98 |
namespace = qualifiedElement.substring(1, closeBrace); |
92 |
qualifiedElement = qualifiedElement.substring(closeBrace+1); |
99 |
qualifiedElement = qualifiedElement.substring(closeBrace + 1); |
93 |
} |
100 |
} |
94 |
// Extract dtd part |
101 |
// Extract dtd part |
95 |
int dtdSlash = qualifiedElement.indexOf('/'); |
102 |
int dtdSlash = qualifiedElement.indexOf('/'); |
96 |
if (dtdSlash > 0) { |
103 |
if (dtdSlash > 0) { |
97 |
dtd = qualifiedElement.substring(dtdSlash+1); |
104 |
dtd = qualifiedElement.substring(dtdSlash + 1); |
98 |
qualifiedElement = qualifiedElement.substring(0, dtdSlash); |
105 |
qualifiedElement = qualifiedElement.substring(0, dtdSlash); |
99 |
} |
106 |
} |
100 |
// Check if the name is a wildcard |
107 |
// Check if the name is a wildcard |
Lines 108-114
Link Here
|
108 |
return nsMatch && elementEquals && dtdEquals; |
115 |
return nsMatch && elementEquals && dtdEquals; |
109 |
} |
116 |
} |
110 |
} |
117 |
} |
111 |
|
118 |
|
112 |
/* (Intentionally not included in javadoc) |
119 |
/* (Intentionally not included in javadoc) |
113 |
* Determines the validation status for the given contents. |
120 |
* Determines the validation status for the given contents. |
114 |
* |
121 |
* |
Lines 120-144
Link Here
|
120 |
* </ul> |
127 |
* </ul> |
121 |
* @throws IOException |
128 |
* @throws IOException |
122 |
*/ |
129 |
*/ |
123 |
private int checkCriteria(InputSource contents) throws IOException { |
130 |
private int checkCriteria(InputSource contents, Map properties) throws IOException { |
124 |
XMLRootHandler xmlHandler = new XMLRootHandler(elementsToFind != null); |
131 |
if (!isProcessed(properties)) |
125 |
try { |
132 |
fillContentProperties(contents, properties); |
126 |
if (!xmlHandler.parseContents(contents)) |
133 |
return checkCriteria(properties); |
127 |
return INDETERMINATE; |
134 |
} |
128 |
} catch (SAXException e) { |
135 |
|
129 |
// we may be handed any kind of contents... it is normal we fail to parse |
136 |
private int checkCriteria(Map properties) throws IOException { |
|
|
137 |
Boolean result = (Boolean) properties.get(RESULT); |
138 |
if (!result.booleanValue()) |
130 |
return INDETERMINATE; |
139 |
return INDETERMINATE; |
131 |
} catch (ParserConfigurationException e) { |
|
|
132 |
// some bad thing happened - force this describer to be disabled |
133 |
String message = ContentMessages.content_parserConfiguration; |
134 |
RuntimeLog.log(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, e)); |
135 |
throw new RuntimeException(message); |
136 |
} |
137 |
// Check to see if we matched our criteria. |
140 |
// Check to see if we matched our criteria. |
138 |
if (elementsToFind != null) { |
141 |
if (elementsToFind != null) { |
139 |
boolean foundOne = false; |
142 |
boolean foundOne = false; |
140 |
for (int i = 0; i < elementsToFind.length && !foundOne; ++i) { |
143 |
for (int i = 0; i < elementsToFind.length && !foundOne; ++i) { |
141 |
foundOne |= elementsToFind[i].matches(xmlHandler.getRootNamespace(), xmlHandler.getRootName(), xmlHandler.getDTD()); |
144 |
String dtd = (String) properties.get(DTD); |
|
|
145 |
String namespace = (String) properties.get(NAMESPACE); |
146 |
String element = (String) properties.get(ELEMENT); |
147 |
foundOne |= elementsToFind[i].matches(namespace, element, dtd); |
142 |
} |
148 |
} |
143 |
if (!foundOne) |
149 |
if (!foundOne) |
144 |
return INDETERMINATE; |
150 |
return INDETERMINATE; |
Lines 151-176
Link Here
|
151 |
* @see IContentDescriber#describe(InputStream, IContentDescription) |
157 |
* @see IContentDescriber#describe(InputStream, IContentDescription) |
152 |
*/ |
158 |
*/ |
153 |
public int describe(InputStream contents, IContentDescription description) throws IOException { |
159 |
public int describe(InputStream contents, IContentDescription description) throws IOException { |
|
|
160 |
return describe(contents, description, new HashMap()); |
161 |
} |
162 |
|
163 |
public int describe(InputStream contents, IContentDescription description, Map properties) throws IOException { |
154 |
// call the basic XML describer to do basic recognition |
164 |
// call the basic XML describer to do basic recognition |
155 |
if (super.describe(contents, description) == INVALID) |
165 |
if (super.describe2(contents, description, properties) == INVALID) |
156 |
return INVALID; |
166 |
return INVALID; |
157 |
// super.describe will have consumed some chars, need to rewind |
167 |
// super.describe will have consumed some chars, need to rewind |
158 |
contents.reset(); |
168 |
contents.reset(); |
159 |
// Check to see if we matched our criteria. |
169 |
// Check to see if we matched our criteria. |
160 |
return checkCriteria(new InputSource(contents)); |
170 |
return checkCriteria(new InputSource(contents), properties); |
161 |
} |
171 |
} |
162 |
|
172 |
|
163 |
/* (Intentionally not included in javadoc) |
173 |
/* (Intentionally not included in javadoc) |
164 |
* @see IContentDescriber#describe(Reader, IContentDescription) |
174 |
* @see IContentDescriber#describe(Reader, IContentDescription) |
165 |
*/ |
175 |
*/ |
166 |
public int describe(Reader contents, IContentDescription description) throws IOException { |
176 |
public int describe(Reader contents, IContentDescription description) throws IOException { |
|
|
177 |
return describe(contents, description, new HashMap()); |
178 |
} |
179 |
|
180 |
public int describe(Reader contents, IContentDescription description, Map properties) throws IOException { |
167 |
// call the basic XML describer to do basic recognition |
181 |
// call the basic XML describer to do basic recognition |
168 |
if (super.describe(contents, description) == INVALID) |
182 |
if (super.describe2(contents, description, properties) == INVALID) |
169 |
return INVALID; |
183 |
return INVALID; |
170 |
// super.describe will have consumed some chars, need to rewind |
184 |
// super.describe will have consumed some chars, need to rewind |
171 |
contents.reset(); |
185 |
contents.reset(); |
172 |
// Check to see if we matched our criteria. |
186 |
// Check to see if we matched our criteria. |
173 |
return checkCriteria(new InputSource(contents)); |
187 |
return checkCriteria(new InputSource(contents), properties); |
|
|
188 |
} |
189 |
|
190 |
static boolean isProcessed(Map properties) { |
191 |
Boolean result = (Boolean) properties.get(RESULT); |
192 |
// It can be set to false which means that content can't be parsed |
193 |
if (result != null) |
194 |
return true; |
195 |
return false; |
196 |
} |
197 |
|
198 |
static void fillContentProperties(InputSource input, Map properties) throws IOException { |
199 |
XMLRootHandler xmlHandler = new XMLRootHandler(true); |
200 |
try { |
201 |
if (!xmlHandler.parseContents(input)) { |
202 |
properties.put(RESULT, new Boolean(false)); |
203 |
return; |
204 |
} |
205 |
} catch (SAXException e) { |
206 |
// we may be handed any kind of contents... it is normal we fail to parse |
207 |
properties.put(RESULT, new Boolean(false)); |
208 |
return; |
209 |
} catch (ParserConfigurationException e) { |
210 |
// some bad thing happened - force this describer to be disabled |
211 |
String message = ContentMessages.content_parserConfiguration; |
212 |
RuntimeLog.log(new Status(IStatus.ERROR, ContentMessages.OWNER_NAME, 0, message, e)); |
213 |
throw new RuntimeException(message); |
214 |
} |
215 |
String element = xmlHandler.getRootName(); |
216 |
if (element != null) |
217 |
properties.put(ELEMENT, element); |
218 |
String dtd = xmlHandler.getDTD(); |
219 |
if (dtd != null) |
220 |
properties.put(DTD, dtd); |
221 |
String namespace = xmlHandler.getRootNamespace(); |
222 |
if (namespace != null) |
223 |
properties.put(NAMESPACE, namespace); |
224 |
properties.put(RESULT, new Boolean(true)); |
174 |
} |
225 |
} |
175 |
|
226 |
|
176 |
/* (Intentionally not included in javadoc) |
227 |
/* (Intentionally not included in javadoc) |