Skip to main content
summaryrefslogtreecommitdiffstats
blob: 89f24f622638d958f13b58d568b21b60a5feb90f (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*******************************************************************************
 * Copyright (c) 2006 Oracle Corporation.
 * 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:
 *    Cameron Bateman/Oracle - initial API and implementation
 *    
 ********************************************************************************/

package org.eclipse.jst.jsf.context.structureddocument.internal.impl;

import java.util.Iterator;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jst.jsf.context.structureddocument.AbstractStructuredDocumentContextFactory;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContext;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContextFactory;
import org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContextFactory2;
import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
import org.eclipse.wst.sse.ui.internal.StructuredTextViewer;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMAttr;
import org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

/**
 * @author cbateman
 *
 */
public class StructuredDocumentContextFactory extends AbstractStructuredDocumentContextFactory
			 implements IStructuredDocumentContextFactory, IStructuredDocumentContextFactory2
{
	/* static attributes */
	private static StructuredDocumentContextFactory  INSTANCE;

	/**
	 * @param supportedDelegateTypes
	 */
	public StructuredDocumentContextFactory(Class[]  supportedDelegateTypes)
	{
	    super(supportedDelegateTypes);
	}
	
	/**
	 * @return an instance (possibly shared) of the this factory
	 */
	public synchronized static StructuredDocumentContextFactory getInstance()
	{
		if (INSTANCE == null)
		{
			INSTANCE = new StructuredDocumentContextFactory();
		}
		
		return INSTANCE;
	}
	
	
	/**
	 * Construct the factory
	 */
	protected StructuredDocumentContextFactory()
	{
		super(new Class[] {IStructuredDocumentContextFactory.class});
	}

	/* (non-Javadoc)
	 * @see org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContextFactory#getContext(org.eclipse.jface.text.ITextViewer, int)
	 */
	public final IStructuredDocumentContext getContext(ITextViewer textViewer, int documentPosition) 
	{
		// first see if I know how to make one
		IStructuredDocumentContext  context = internalGetContext(textViewer, documentPosition);
		
		// if I don't know, ask my delegates
		if (context == null)
		{
			context = delegateGetContext(textViewer, documentPosition);
		}
		
		return context;
	}

	private IStructuredDocumentContext internalGetContext(ITextViewer textViewer, int documentPosition)
	{
		if (textViewer instanceof StructuredTextViewer)
		{
			IDocument document = ((StructuredTextViewer)textViewer).getDocument();
			return internalGetContext(document, documentPosition);
		}
		
		return null;
	}
	
	private IStructuredDocumentContext internalGetContext(IDocument document, int documentPosition)
	{
		if (document instanceof IStructuredDocument)
		{
			return new DefaultStructuredDocumentContext((IStructuredDocument)document, documentPosition);
		}
		
		return null;
	}
	
	private IStructuredDocumentContext delegateGetContext(ITextViewer textViewer, int documentPosition)
	{
		synchronized(_delegates)
		{
			for (final Iterator it = _delegates.iterator(); it.hasNext();)
			{
				IStructuredDocumentContextFactory delegateFactory = (IStructuredDocumentContextFactory) ((IAdaptable) it.next()).getAdapter(IStructuredDocumentContextFactory.class);
				IStructuredDocumentContext context = delegateFactory.getContext(textViewer, documentPosition);
				
				if (context != null)
				{
					return context;
				}
			}
			
			return null;
		}
	}

	/**
	 * @see org.eclipse.jst.jsf.context.structureddocument.IStructuredDocumentContextFactory#getContext(org.eclipse.jface.text.IDocument, int)
	 */
	public IStructuredDocumentContext getContext(IDocument document, int documentPosition) {
		// first see if I know how to make one
		IStructuredDocumentContext  context = internalGetContext(document, documentPosition);
		
		// if I don't know, ask my delegates
		if (context == null)
		{
			context = delegateGetContext(document, documentPosition);
		}
		
		return context;
	}
	
	private IStructuredDocumentContext delegateGetContext(IDocument document, int documentPosition)
	{
		synchronized(_delegates)
		{
			for (final Iterator it = _delegates.iterator(); it.hasNext();)
			{
				IStructuredDocumentContextFactory delegateFactory = (IStructuredDocumentContextFactory) ((IAdaptable) it.next()).getAdapter(IStructuredDocumentContextFactory.class);
				IStructuredDocumentContext context = delegateFactory.getContext(document, documentPosition);
				
				if (context != null)
				{
					return context;
				}
			}
			
			return null;
		}
	}

    public IStructuredDocumentContext getContext(IDocument document, Node node) 
    {
        // first see if I know how to make one
        IStructuredDocumentContext  context = internalGetContext(document, node);
        
        // if I don't know, ask my delegates
        if (context == null)
        {
            context = delegateGetContext(document, node);
        }
        
        return context;

    }
    


	private IStructuredDocumentContext internalGetContext(IDocument document, Node node)
    {
        if (document instanceof IStructuredDocument)
        {
            final IStructuredDocument sDoc = (IStructuredDocument) document;
            if (node instanceof IndexedRegion)
            {
                final int position = ((IndexedRegion)node).getStartOffset();
                return new DefaultStructuredDocumentContext(sDoc, position);
            }
            else if (node instanceof IDOMAttr)
            {
                IDOMAttr  attr = (IDOMAttr) node;
                final int position = attr.getValueRegionStartOffset();
                return new DefaultStructuredDocumentContext(sDoc, position);
            }
        }
        
        return null;
    }
    
    private IStructuredDocumentContext delegateGetContext(IDocument document, Node node)
    {
        synchronized(_delegates)
        {
            for (final Iterator it = _delegates.iterator(); it.hasNext();)
            {
                IStructuredDocumentContextFactory delegateFactory = (IStructuredDocumentContextFactory) ((IAdaptable) it.next()).getAdapter(IStructuredDocumentContextFactory.class);
                IStructuredDocumentContext context = delegateFactory.getContext(document, node);
                
                if (context != null)
                {
                    return context;
                }
            }
            
            return null;
        }
    }
    
    public IStructuredDocumentContext getContext(Element element) 
    {
        // first see if I know how to make one
        IStructuredDocumentContext  context = internalGetContext(element);
        
        // if I don't know, ask my delegates
        if (context == null)
        {
            context = delegateGetContext(element);
        }
        
        return context;

    }
    
	private IStructuredDocumentContext internalGetContext(Element element) {
		if (element instanceof IDOMNode){
			final IDOMNode node = (IDOMNode)element;
			final IStructuredDocument sDoc = node.getStructuredDocument();
            final int position = node.getStartOffset();
            
            return new DefaultStructuredDocumentContext(sDoc, position);            
        }
		
		return null;
	}
	
    private IStructuredDocumentContext delegateGetContext(Element element) {
        synchronized(_delegates)
        {
            for (final Iterator it = _delegates.iterator(); it.hasNext();)
            {
                IStructuredDocumentContextFactory2 delegateFactory = (IStructuredDocumentContextFactory2) ((IAdaptable) it.next()).getAdapter(IStructuredDocumentContextFactory2.class);
                if (delegateFactory != null)
                {
	                IStructuredDocumentContext context = delegateFactory.getContext(element);
	                
	                if (context != null)
	                {
	                    return context;
	                }
                }
            }
            
            return null;
        }
	}


}

Back to the top