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.

aboutsummaryrefslogtreecommitdiffstats
blob: 21bf47691bed58a63ebc79b006755e520e68bce3 (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.util;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.wst.dtd.core.internal.Attribute;
import org.eclipse.wst.dtd.core.internal.AttributeList;
import org.eclipse.wst.dtd.core.internal.CMBasicNode;
import org.eclipse.wst.dtd.core.internal.DTDFile;
import org.eclipse.wst.dtd.core.internal.DTDNode;
import org.eclipse.wst.dtd.core.internal.Element;
import org.eclipse.wst.dtd.core.internal.Entity;
import org.eclipse.wst.dtd.core.internal.ParameterEntityReference;


// this class is responsible for updating the model when items
// are deleted or a external parm entity changes so that
// items referenced by it are cleaned up
// note that top level nodes are queued up for deletion so that
// iteration over the list of nodes from the dtdfile is not messed up
public class DTDModelUpdater extends DTDVisitor {

	protected boolean isParmEntity = false;

	protected boolean isUpdating = false;

	protected List nodesToDelete = new ArrayList();
	protected DTDNode nodeToDelete;
	protected String oldRefName = ""; //$NON-NLS-1$
	protected Object requestor;

	public DTDModelUpdater() {

	}

	public synchronized void objectAboutToBeDeleted(Object requestor, DTDNode node) {
		if (isUpdating) {
			return;
		}
		if (!(node instanceof Entity || node instanceof Element)) {
			// just ignore if it is not one of these
			return;
		}
		if (node instanceof Entity && !((Entity) node).isParameterEntity()) {
			// if it is not a parameter entity, ignore as well
			return;
		}


		isUpdating = true;
		this.requestor = requestor;
		this.nodeToDelete = node;
		oldRefName = node.getName();
		isParmEntity = false;
		nodesToDelete.clear();

		if (node instanceof Entity) {
			isParmEntity = true;
			oldRefName = "%" + oldRefName + ";"; //$NON-NLS-1$ //$NON-NLS-2$
		}

		DTDFile dtdFile = node.getDTDFile();
		visit(dtdFile);

		for (int i = 0; i < nodesToDelete.size(); i++) {
			dtdFile.deleteNode(requestor, (DTDNode) nodesToDelete.get(i));
		}

		isUpdating = false;
	}

	public void visitAttribute(Attribute attr) {
		super.visitAttribute(attr);
		if (isParmEntity) {
			if (attr.getName().equals(oldRefName)) {
				attr.setName(requestor, "TempName"); //$NON-NLS-1$
			}
			if (attr.getType().equals(oldRefName)) {
				attr.setType(requestor, Attribute.CDATA);
			}
		}
		// check the attr name and the attr type to see if it
		// needs updating
	}

	public void visitAttributeList(AttributeList attList) {
		super.visitAttributeList(attList);
		if (attList.getName().equals(oldRefName)) {
			if (isParmEntity) {
				attList.setName(requestor, "TempName"); //$NON-NLS-1$
			}
			else {
				// save up for later deletion
				nodesToDelete.add(attList);
			}
		}
	}

	public void visitElement(Element element) {
		if (isParmEntity) {
			if (element.getName().equals(oldRefName)) {
				element.setName(requestor, "TempName"); //$NON-NLS-1$
			}
		}
		super.visitElement(element);
	}

	public void visitExternalParameterEntityReference(ParameterEntityReference parmEntityRef) {
		super.visitExternalParameterEntityReference(parmEntityRef);
		if (isParmEntity && parmEntityRef.getName().equals(oldRefName)) {
			nodesToDelete.add(parmEntityRef);
		}
	}

	public void visitReference(CMBasicNode node) {
		super.visitReference(node);

		if (node.getName().equals(oldRefName)) {
			DTDNode parent = (DTDNode) node.getParentNode();
			parent.delete(requestor, node);
		}
	}
}

Back to the top