blob: ae22b6a8f9133d1e60a299d303f72574b429ada6 [file] [log] [blame]
david_williamscfdb2cd2004-11-11 08:37:49 +00001/*******************************************************************************
2 * Copyright (c) 2001, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 * Jens Lukowski/Innoopract - initial renaming/restructuring
11 *
12 *******************************************************************************/
nitindd6e591d2005-03-14 22:21:57 +000013package org.eclipse.wst.dtd.core.internal;
david_williamscfdb2cd2004-11-11 08:37:49 +000014
15import java.util.ArrayList;
16
17import org.eclipse.swt.graphics.Image;
nitindd6e591d2005-03-14 22:21:57 +000018import org.eclipse.wst.dtd.core.internal.parser.DTDRegionTypes;
david_williamscfdb2cd2004-11-11 08:37:49 +000019import org.eclipse.wst.dtd.core.internal.text.RegionIterator;
david_williams4ad020f2005-04-18 08:00:30 +000020import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
21import org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion;
david_williamscfdb2cd2004-11-11 08:37:49 +000022
23
24public class AttributeList extends NamedTopLevelNode {
25 public AttributeList(DTDFile file, IStructuredDocumentRegion flatNode) {
26 super(file, flatNode, DTDRegionTypes.ATTLIST_TAG);
27 }
28
29 public void addAttribute(String name) {
david_williams38046012005-04-08 19:04:29 +000030 beginRecording(this, DTDCoreMessages._UI_LABEL_ATTR_LIST_ADD); //$NON-NLS-1$
david_williamscfdb2cd2004-11-11 08:37:49 +000031
32 DTDNode lastAttribute = (DTDNode) getLastChild();
33 if (lastAttribute != null) {
34 replaceText(this, lastAttribute.getEndOffset(), 0, "\n\t" + name + " CDATA #IMPLIED"); //$NON-NLS-1$ //$NON-NLS-2$
nitindd6e591d2005-03-14 22:21:57 +000035 }
36 else {
david_williamscfdb2cd2004-11-11 08:37:49 +000037 ITextRegion nameRegion = getNameRegion();
38 if (nameRegion != null) {
david_williams29d69682004-11-20 17:49:29 +000039 replaceText(this, getStructuredDTDDocumentRegion().getEndOffset(nameRegion), 0, "\n\t" + name + " CDATA #IMPLIED"); //$NON-NLS-1$ //$NON-NLS-2$
david_williamscfdb2cd2004-11-11 08:37:49 +000040 }
41
42 }
43
44 endRecording(this);
45 }
46
47 public Image getImage() {
david_williams425ffe72004-12-07 21:46:39 +000048 return DTDCorePlugin.getInstance().getImage(DTDResource.ATTRIBUTELISTICON);
david_williamscfdb2cd2004-11-11 08:37:49 +000049 }
50
51 public void insertIntoModel(Object requestor, Attribute reference, Attribute node, boolean isAfter) {
52 int offset = 0;
53 String newText = ""; //$NON-NLS-1$
54 String nodeText = node.getFullNodeText();
david_williamscfdb2cd2004-11-11 08:37:49 +000055 if (!isAfter) {
56 offset = reference.getStartOffset();
nitindd6e591d2005-03-14 22:21:57 +000057 }
58 else {
david_williamscfdb2cd2004-11-11 08:37:49 +000059 // try and get next child
60 Attribute attr = (Attribute) reference.getNextSibling();
61 if (attr != null) {
62 offset = attr.getStartOffset();
nitindd6e591d2005-03-14 22:21:57 +000063 }
64 else {
david_williamscfdb2cd2004-11-11 08:37:49 +000065 // just use the end offset
66 offset = reference.getWhitespaceEndOffset();
67 }
68 }
69 newText += nodeText;// + (isLastChild ? "\n" : "\n\t");
70 if (!node.hasTrailingWhitespace()) {
71 newText += "\n\t"; //$NON-NLS-1$
72 }
73 replaceText(requestor, offset, 0, newText);
74 }
75
76 public void resolveRegions() {
77 removeChildNodes();
78 RegionIterator iter = iterator();
79
80 if (getNameRegion() != null) {
81 // we skip past the name token is our name
82 skipPastName(iter);
83 }
84
85 ArrayList children = new ArrayList();
86 Attribute attribute = null;
87 boolean trailingWhitespace = false;
88 while (iter.hasNext()) {
89 ITextRegion currentRegion = iter.next();
90 if (currentRegion.getType().equals(DTDRegionTypes.ATTRIBUTE_NAME)) {
david_williams29d69682004-11-20 17:49:29 +000091 attribute = new Attribute(getDTDFile(), getStructuredDTDDocumentRegion());
david_williamscfdb2cd2004-11-11 08:37:49 +000092 children.add(attribute);
93 appendChild(attribute);
94 trailingWhitespace = false;
95 }
96 if (attribute != null && currentRegion.getType() != DTDRegionTypes.END_TAG) {
97 if (!trailingWhitespace) {
98 attribute.addRegion(currentRegion);
nitindd6e591d2005-03-14 22:21:57 +000099 }
100 else {
david_williamscfdb2cd2004-11-11 08:37:49 +0000101 if (currentRegion.getType() == DTDRegionTypes.WHITESPACE) {
102 attribute.addWhitespaceRegion(currentRegion);
103 }
104 }
105
106 // the following prevents extra whitespace from being picked
107 // up by the attribute
108 if (currentRegion.getType() == DTDRegionTypes.REQUIRED_KEYWORD || currentRegion.getType() == DTDRegionTypes.IMPLIED_KEYWORD || currentRegion.getType() == DTDRegionTypes.SINGLEQUOTED_LITERAL || currentRegion.getType() == DTDRegionTypes.DOUBLEQUOTED_LITERAL) {
109 trailingWhitespace = true;
110 }
111 }
112 }
113 int numKids = children.size();
114 for (int i = 0; i < numKids; i++) {
115 ((Attribute) children.get(i)).resolveRegions();
116 } // end of for ()
117 }
118}