Skip to main content

This CGIT instance is deprecated, and repositories have been moved to Gitlab or Github. See the repository descriptions for specific locations.

summaryrefslogtreecommitdiffstats
blob: 4e3cea7b42fc86b189a546b8eb012a8a12d31adb (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*******************************************************************************
 * Copyright (c) 2001, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Jens Lukowski/Innoopract - initial renaming/restructuring
 *     
 *******************************************************************************/
package org.eclipse.wst.dtd.core.internal;

import org.eclipse.wst.dtd.core.internal.parser.DTDRegionTypes;
import org.eclipse.wst.dtd.core.internal.text.RegionIterator;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;


// base class for nodes that can contain external content
public abstract class ExternalNode extends NamedTopLevelNode {

	String publicID;

	String systemID;

	public ExternalNode(DTDFile file, IStructuredDocumentRegion flatNode, String tagType) {
		super(file, flatNode, tagType);
	}

	public ITextRegion getNextQuotedLiteral(RegionIterator iter) {
		while (iter.hasNext()) {
			ITextRegion region = iter.next();
			if (region.getType().equals(DTDRegionTypes.SINGLEQUOTED_LITERAL) || region.getType().equals(DTDRegionTypes.DOUBLEQUOTED_LITERAL)) {
				return region;
			}
		}
		return null;
	}

	/**
	 * Get the value of publicID.
	 * 
	 * @return value of publicID.
	 */
	public String getPublicID() {
		ITextRegion publicValue = getPublicValueRegion();
		if (publicValue != null) {
			return getValueFromQuotedRegion(publicValue);
		}
		return ""; //$NON-NLS-1$
	}

	public ITextRegion getPublicKeywordRegion(RegionIterator iter) {
		return getNextRegion(iter, DTDRegionTypes.PUBLIC_KEYWORD);
	}

	public ITextRegion getPublicValueRegion() {
		RegionIterator iter = iterator();

		ITextRegion publicKeyword = getPublicKeywordRegion(iter);

		if (publicKeyword != null && iter.hasNext()) {
			ITextRegion quotedLiteral = getNextQuotedLiteral(iter);
			if (quotedLiteral != null) {
				return quotedLiteral;
			}
		}
		return null;
	}

	/**
	 * Get the value of systemID.
	 * 
	 * @return value of systemID.
	 */
	public String getSystemID() {
		ITextRegion systemValue = getSystemValueRegion();
		if (systemValue != null) {
			return getValueFromQuotedRegion(systemValue);
		}
		return ""; //$NON-NLS-1$
	}

	public ITextRegion getSystemKeywordRegion(RegionIterator iter) {
		return getNextRegion(iter, DTDRegionTypes.SYSTEM_KEYWORD);
	}

	public ITextRegion getSystemValueRegion() {
		RegionIterator iter = iterator();

		ITextRegion systemKeyword = getSystemKeywordRegion(iter);
		if (systemKeyword != null && iter.hasNext()) {
			ITextRegion quotedLiteral = getNextQuotedLiteral(iter);
			if (quotedLiteral != null) {
				return quotedLiteral;
			}
		}
		else {
			// try and see if there is a second quoted literal after a public
			// keyword
			iter = iterator();
			ITextRegion publicKeyword = getPublicKeywordRegion(iter);

			if (publicKeyword != null && iter.hasNext()) {
				ITextRegion quotedLiteral = getNextQuotedLiteral(iter);
				if (quotedLiteral != null && iter.hasNext()) {
					// now get the second quoted literal
					quotedLiteral = getNextQuotedLiteral(iter);
					if (quotedLiteral != null) {
						// got it!
						return quotedLiteral;
					}
				}
			}
		}
		return null;
	}

	public String getValueFromQuotedRegion(ITextRegion region) {
		String type = region.getType();
		if (type.equals(DTDRegionTypes.SINGLEQUOTED_LITERAL) || type.equals(DTDRegionTypes.DOUBLEQUOTED_LITERAL)) {
			String text = getStructuredDTDDocumentRegion().getText(region);
			return text.substring(1, text.length() - 1);
		}
		return ""; //$NON-NLS-1$
	}

	/**
	 * Set the value of publicID.
	 * 
	 * @param v
	 *            Value to assign to publicID.
	 */
	public void setPublicID(Object requestor, String v) {
		if (!v.equals(publicID)) {
			publicID = v;
			ITextRegion publicValue = getPublicValueRegion();
			ITextRegion publicKeyword = getPublicKeywordRegion(iterator());
			ITextRegion systemKeyword = getSystemKeywordRegion(iterator());
			ITextRegion systemValue = getSystemValueRegion();

			if (v.equals("")) { //$NON-NLS-1$
				if (publicKeyword != null) {
					// time to get rid of the public keyword and value
					// and replace it with the system one
					int startOffset = getStructuredDTDDocumentRegion().getStartOffset(publicKeyword);
					String newString = "SYSTEM"; //$NON-NLS-1$
					if (systemValue == null) {
						newString += " \"\""; //$NON-NLS-1$
					}
					replaceText(requestor, startOffset, getStructuredDTDDocumentRegion().getEndOffset(publicValue) - startOffset, newString);
				}
			}
			else {
				// here were setting a non empty value
				String quoteChar = v.indexOf("\"") == -1 ? "\"" : "'"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
				if (publicValue != null) {
					replaceText(requestor, getStructuredDTDDocumentRegion().getStartOffset(publicValue), publicValue.getLength(), quoteChar + publicID + quoteChar);
				}
				else {
					// time to create stuff
					if (publicKeyword != null) {
						// then just put our new value after the keyword
						replaceText(requestor, getStructuredDTDDocumentRegion().getEndOffset(publicKeyword), 0, " " + quoteChar + v + quoteChar); //$NON-NLS-1$
					}
					else {
						// we need the public keyword as well
						if (systemKeyword != null) {
							replaceText(requestor, getStructuredDTDDocumentRegion().getStartOffset(systemKeyword), systemKeyword.getLength(), "PUBLIC " + quoteChar + v + quoteChar); //$NON-NLS-1$
						}
						else {
							ITextRegion nameRegion = getNameRegion();
							replaceText(requestor, getStructuredDTDDocumentRegion().getEndOffset(nameRegion), 0, " PUBLIC " + quoteChar + v + quoteChar); //$NON-NLS-1$
						}
					}
				}
			}
		}
	}


	/**
	 * Set the value of publicID.
	 * 
	 * @param v
	 *            Value to assign to publicID.
	 */
	public void setPublicID(String v) {
		beginRecording(this, DTDCoreMessages._UI_LABEL_EXT_NODE_PUBLIC_ID_CHG); //$NON-NLS-1$
		setPublicID(this, v);
		endRecording(this);
	}

	/**
	 * Set the value of systemID.
	 * 
	 * @param v
	 *            Value to assign to systemID.
	 */
	public void setSystemID(Object requestor, String v) {
		if (!v.equals(systemID) || (getPublicKeywordRegion(iterator()) == null && getSystemKeywordRegion(iterator()) == null)) {
			systemID = v;
			String quoteChar = v.indexOf("\"") == -1 ? "\"" : "'"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
			ITextRegion systemValue = getSystemValueRegion();
			if (systemValue != null) {
				replaceText(requestor, getStructuredDTDDocumentRegion().getStartOffset(systemValue), systemValue.getLength(), quoteChar + systemID + quoteChar);
			}
			else {
				ITextRegion systemKeyword = getSystemKeywordRegion(iterator());

				// time to create stuff
				if (systemKeyword != null) {
					// then just put our new value after the keyword
					replaceText(requestor, getStructuredDTDDocumentRegion().getEndOffset(systemKeyword), 0, " " + quoteChar + v + quoteChar); //$NON-NLS-1$
				}
				else {
					// see if we have a public keyword
					ITextRegion publicKeyword = getPublicKeywordRegion(iterator());
					if (publicKeyword == null) {
						ITextRegion nameRegion = getNameRegion();

						replaceText(requestor, getStructuredDTDDocumentRegion().getEndOffset(nameRegion), 0, " SYSTEM " + quoteChar + v + quoteChar); //$NON-NLS-1$
					}
					else {
						// put it after the public value region
						ITextRegion publicValueRegion = getPublicValueRegion();
						replaceText(requestor, getStructuredDTDDocumentRegion().getEndOffset(publicValueRegion), 0, " " + quoteChar + v + quoteChar); //$NON-NLS-1$
					}

				}
			}

		}
	}


	/**
	 * Set the value of systemID.
	 * 
	 * @param v
	 *            Value to assign to systemID.
	 */
	public void setSystemID(String v) {
		beginRecording(this, DTDCoreMessages._UI_LABEL_EXT_NODE_SYSTEM_ID_CHG); //$NON-NLS-1$
		setSystemID(this, v);
		endRecording(this);
	}
}

Back to the top