Lines 44-54
Link Here
|
44 |
package org.netbeans.modules.xml.schema.completion.util; |
44 |
package org.netbeans.modules.xml.schema.completion.util; |
45 |
|
45 |
|
46 |
import java.net.URI; |
46 |
import java.net.URI; |
47 |
import java.util.ArrayList; |
47 |
import java.util.*; |
48 |
import java.util.HashMap; |
|
|
49 |
import java.util.List; |
50 |
import java.util.Stack; |
51 |
import java.util.StringTokenizer; |
52 |
import java.util.logging.Level; |
48 |
import java.util.logging.Level; |
53 |
import java.util.logging.Logger; |
49 |
import java.util.logging.Logger; |
54 |
import java.util.regex.Pattern; |
50 |
import java.util.regex.Pattern; |
Lines 63-89
Link Here
|
63 |
import org.netbeans.api.lexer.TokenSequence; |
59 |
import org.netbeans.api.lexer.TokenSequence; |
64 |
import org.netbeans.api.xml.lexer.XMLTokenId; |
60 |
import org.netbeans.api.xml.lexer.XMLTokenId; |
65 |
import org.netbeans.editor.BaseDocument; |
61 |
import org.netbeans.editor.BaseDocument; |
66 |
import org.netbeans.modules.xml.axi.AXIComponent; |
62 |
import org.netbeans.modules.xml.axi.*; |
67 |
import org.netbeans.modules.xml.axi.AXIDocument; |
|
|
68 |
import org.netbeans.modules.xml.axi.AXIModel; |
69 |
import org.netbeans.modules.xml.axi.AXIModelFactory; |
70 |
import org.netbeans.modules.xml.axi.AXIType; |
71 |
import org.netbeans.modules.xml.axi.AbstractAttribute; |
72 |
import org.netbeans.modules.xml.axi.AbstractElement; |
73 |
import org.netbeans.modules.xml.axi.AnyAttribute; |
74 |
import org.netbeans.modules.xml.axi.AnyElement; |
75 |
import org.netbeans.modules.xml.axi.Attribute; |
76 |
import org.netbeans.modules.xml.axi.Element; |
77 |
import org.netbeans.modules.xml.axi.datatype.Datatype; |
63 |
import org.netbeans.modules.xml.axi.datatype.Datatype; |
78 |
import org.netbeans.modules.xml.schema.completion.*; |
64 |
import org.netbeans.modules.xml.schema.completion.*; |
79 |
import org.netbeans.modules.xml.schema.completion.spi.CompletionModelProvider.CompletionModel; |
65 |
import org.netbeans.modules.xml.schema.completion.spi.CompletionModelProvider.CompletionModel; |
80 |
import org.netbeans.modules.xml.schema.model.Form; |
66 |
import org.netbeans.modules.xml.schema.model.Form; |
|
|
67 |
import org.netbeans.modules.xml.schema.model.GlobalElement; |
68 |
import org.netbeans.modules.xml.schema.model.SchemaComponent; |
69 |
import org.netbeans.modules.xml.schema.model.SchemaModel; |
70 |
import org.netbeans.modules.xml.schema.model.visitor.FindSubstitutions; |
81 |
import org.openide.filesystems.FileObject; |
71 |
import org.openide.filesystems.FileObject; |
82 |
import org.openide.loaders.DataObject; |
72 |
import org.openide.loaders.DataObject; |
83 |
import org.openide.windows.TopComponent; |
73 |
import org.openide.windows.TopComponent; |
84 |
|
74 |
|
85 |
/** |
75 |
/** |
86 |
* |
76 |
* Utility class containing methods to query the model for completion suggestions. |
|
|
77 |
* |
87 |
* @author Samaresh (Samaresh.Panda@Sun.Com) |
78 |
* @author Samaresh (Samaresh.Panda@Sun.Com) |
88 |
*/ |
79 |
*/ |
89 |
public class CompletionUtil { |
80 |
public class CompletionUtil { |
Lines 243-270
Link Here
|
243 |
} |
234 |
} |
244 |
|
235 |
|
245 |
/** |
236 |
/** |
246 |
* Returns the list of child-elements for a given element. |
237 |
* Returns the list of child elements for a given element. This includes |
|
|
238 |
* all available substitutions for child elements from all |
239 |
* {@link CompletionModel}s in the specified completion context. It excludes |
240 |
* abstract elements |
241 |
* @param context the completion context for which to find all valid |
242 |
* elements for insertion |
243 |
* @return all elements appropriate to be inserted in the specified context, |
244 |
* or {@code null} if no parent element can be found for the specified |
245 |
* context. |
247 |
*/ |
246 |
*/ |
248 |
public static List<CompletionResultItem> getElements( |
247 |
public static List<CompletionResultItem> getElements(CompletionContextImpl context) { |
249 |
CompletionContextImpl context) { |
|
|
250 |
Element element = findAXIElementAtContext(context); |
248 |
Element element = findAXIElementAtContext(context); |
251 |
if(element == null) |
249 |
if(element == null) { |
252 |
return null; |
250 |
return null; |
|
|
251 |
} |
253 |
|
252 |
|
254 |
List<CompletionResultItem> results = new ArrayList<CompletionResultItem>(); |
253 |
List<CompletionResultItem> results = new ArrayList<CompletionResultItem>(); |
255 |
for(AbstractElement ae: element.getChildElements()) { |
254 |
for(AbstractElement ae: element.getChildElements()) { |
256 |
AXIComponent original = ae.getOriginal(); |
255 |
AXIComponent original = ae.getOriginal(); |
257 |
if(original.getTargetNamespace() == null) { //no namespace |
256 |
if(original.getTargetNamespace() == null) { //no namespace |
258 |
CompletionResultItem item = createResultItem(original, null, context); |
257 |
CompletionResultItem item = createResultItem(original, null, context); |
259 |
if(item != null) |
258 |
if(item != null) { |
260 |
results.add(item); |
259 |
results.add(item); |
261 |
continue; |
260 |
} |
262 |
} |
261 |
} else if(original instanceof AnyElement) { |
263 |
if(original instanceof AnyElement) { |
|
|
264 |
results.addAll(substituteAny((AnyElement)original, context)); |
262 |
results.addAll(substituteAny((AnyElement)original, context)); |
265 |
continue; |
263 |
} else if(original instanceof Element) { |
|
|
264 |
Element childElement = (Element) original; |
265 |
if(!childElement.getAbstract()) { |
266 |
addNSAwareCompletionItems(original, context, null, results); |
267 |
} |
268 |
addSubstitutionCompletionItems(childElement, context, results); |
266 |
} |
269 |
} |
267 |
addNSAwareCompletionItems(original,context,null,results); |
|
|
268 |
} |
270 |
} |
269 |
return results; |
271 |
return results; |
270 |
} |
272 |
} |
Lines 329-334
Link Here
|
329 |
} |
331 |
} |
330 |
return result; |
332 |
return result; |
331 |
} |
333 |
} |
|
|
334 |
|
335 |
/** |
336 |
* Find all possible substitutions for the specified {@link Element} within |
337 |
* the specified {@link CompletionContextImpl completion context}. |
338 |
* @param forSubstitution the Element to find substitutions for. |
339 |
* @param context the context to use to find available substitutions. All |
340 |
* available {@link CompletionModel}s' {@link SchemaModel}s will be searched. |
341 |
* @param results the result set to add the results to. |
342 |
*/ |
343 |
private static void addSubstitutionCompletionItems(Element forSubstitution, CompletionContextImpl context, List<CompletionResultItem> results) { |
344 |
AXIModel model = forSubstitution.getModel(); |
345 |
String nsUri = forSubstitution.getTargetNamespace(); |
346 |
String localName = forSubstitution.getName(); |
347 |
for (CompletionModel completionModel : context.getCompletionModels()) { |
348 |
SchemaModel schemaModel = completionModel.getSchemaModel(); |
349 |
Set<GlobalElement> substitutions = FindSubstitutions.resolveSubstitutions(schemaModel, nsUri, localName); |
350 |
for (GlobalElement substitution : substitutions) { |
351 |
AXIComponent substitutionElement = getAxiComponent(model, substitution); |
352 |
addNSAwareCompletionItems(substitutionElement, context, completionModel, results); |
353 |
} |
354 |
} |
355 |
} |
356 |
|
357 |
|
358 |
/** |
359 |
* Finds the {@link AXIComponent} representing the specified |
360 |
* {@link SchemaComponent}, from the specified {@link AXIModel}. |
361 |
* Modified from org.netbeans.modules.xml.axi.impl.Util.lookup(), |
362 |
* org.netbeans.modules.xml.axi.impl.AXIModelImpl.lookup(), |
363 |
* and org.netbeans.modules.xml.axi.impl.AXIModelImpl.lookupFromOtherModel(). |
364 |
* @param model the model to search in to find the representation of the |
365 |
* specified {@link SchemaComponent} |
366 |
* @param schemaComponent the {@link SchemaComponent} to search for a |
367 |
* representation of |
368 |
* @return the AXI representation of the specified schema component, or null |
369 |
* if no AXI representation could be found for the schema component. |
370 |
*/ |
371 |
private static AXIComponent getAxiComponent(AXIModel model, SchemaComponent schemaComponent) { |
372 |
if(model.getSchemaModel() == schemaComponent.getModel()) { |
373 |
return findChild(model.getRoot(), schemaComponent); |
374 |
} |
375 |
if(!schemaComponent.isInDocumentModel()) { |
376 |
return null; |
377 |
} |
378 |
AXIModelFactory factory = AXIModelFactory.getDefault(); |
379 |
AXIModel otherModel = factory.getModel(schemaComponent.getModel()); |
380 |
return otherModel == null ? null : findChild(otherModel.getRoot(), schemaComponent); |
381 |
} |
382 |
|
383 |
/** |
384 |
* Finds an {@link AXIComponent} representation of the specified |
385 |
* {@link SchemaComponent}, by searching the children of the specified |
386 |
* {@link AXIDocument}. |
387 |
* Adapted from org.netbeans.modules.xml.axi.impl.AXIDocumentImpl.findChild(). |
388 |
* @param document the document to search through to find the representation |
389 |
* of the specified schema component |
390 |
* @param child the schema component whose representation to find in the |
391 |
* specified document |
392 |
* @return the AXI representation of the specified schema component, from |
393 |
* the specified document, or {@code null} if the schema component has no |
394 |
* representation in the children of the document. |
395 |
*/ |
396 |
private static AXIComponent findChild(AXIDocument document, SchemaComponent child) { |
397 |
for(AXIComponent childRepresentation : document.getChildren()) { |
398 |
if(childRepresentation.getPeer() == child) { |
399 |
return childRepresentation; |
400 |
} |
401 |
} |
402 |
return null; |
403 |
} |
332 |
|
404 |
|
333 |
private static void addNSAwareCompletionItems(AXIComponent axi, |
405 |
private static void addNSAwareCompletionItems(AXIComponent axi, |
334 |
CompletionContextImpl context, CompletionModel cm, List<CompletionResultItem> results) { |
406 |
CompletionContextImpl context, CompletionModel cm, List<CompletionResultItem> results) { |