Skip to main content
summaryrefslogtreecommitdiffstats
blob: a230cd2e16e209b4e444b4bd144d16c19addcc35 (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
/*******************************************************************************
 * Copyright (c) 2001, 2004 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;


// interface for nodes that can exist at the top level in a dtdfile
// eg. entity, notation, element, comment, attlist, or unrecognized stuff (ie
// <junk dkfjdl>
public abstract class NamedTopLevelNode extends TopLevelNode {

	private String tagStartType;

	public NamedTopLevelNode(DTDFile dtdFile, IStructuredDocumentRegion flatNode, String type) {
		super(dtdFile, flatNode);
		tagStartType = type;
	}

	public ITextRegion getNameRegion() {
		// name region is located after the whitespace (which is after
		// the elementtag
		RegionIterator iter = iterator();
		getNextRegion(iter, tagStartType);

		if (!iter.hasNext()) {
			return null;
		}

		ITextRegion region = iter.next();
		if (!region.getType().equals(DTDRegionTypes.WHITESPACE)) {
			return null;
		}

		if (!iter.hasNext()) {
			return null;
		}

		region = iter.next();
		if (region.getType().equals(DTDRegionTypes.NAME)) {
			return region;
		}

		// we normally stop here, but for entities, we have to see if we are
		// at a '%'. if so, we skip that and find the name after the
		// whitespace again
		if (tagStartType == DTDRegionTypes.ENTITY_TAG && region.getType().equals(DTDRegionTypes.PERCENT) && iter.hasNext()) {
			region = iter.next();

			if (!region.getType().equals(DTDRegionTypes.WHITESPACE)) {
				return null;
			}

			if (!iter.hasNext()) {
				return null;
			}

			region = iter.next();
			if (region.getType().equals(DTDRegionTypes.NAME)) {
				return region;
			}
		}

		return null;
	}

	public ITextRegion getStartTag(RegionIterator iter) {
		return getNextRegion(iter, tagStartType);
	}

	public ITextRegion getWhitespaceAfterName() {
		ITextRegion nameRegion = getNameRegion();
		RegionIterator iter = iterator();
		// skip past the element tag region
		getNextRegion(iter, tagStartType);
		boolean foundName = false;
		while (iter.hasNext()) {
			ITextRegion region = iter.next();
			if (!foundName && nameRegion != null && region == nameRegion) {
				foundName = true;
			}

			if (region.getType().equals(DTDRegionTypes.WHITESPACE)) {
				// there is no name region or we have already passed it
				if (nameRegion == null || foundName) {
					return region;
				}
			}
		}
		return null;
	}

	public void setName(Object requestor, String name) {
		ITextRegion nameRegion = getNameRegion();
		if (nameRegion != null) {
			super.setName(requestor, name);
		}
		else {
			RegionIterator iter = iterator();
			ITextRegion elementTagRegion = getNextRegion(iter, tagStartType);
			int replaceLength = 0;
			if (iter.hasNext()) {
				ITextRegion region = iter.next();
				if (region.getType().equals(DTDRegionTypes.WHITESPACE)) {
					if (region.getLength() >= 2) {
						// there are 2 spaces between 'ELEMENT' and the
						// content
						// Change replace length to 1 so that the new name and
						// the content are separated by a single space
						replaceLength = 1;
					}
				}
			}

			// beginRecording(requestor, "Name Change");
			String newText = " " + name; //$NON-NLS-1$
			replaceText(requestor, getStructuredDTDDocumentRegion().getEndOffset(elementTagRegion), replaceLength, newText);
			// endRecording(requestor);
		}
	}

}

Back to the top