use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class ModelQueryAdapterFactoryForXML method createAdapter.
/**
* createAdapter method comment.
*/
protected INodeAdapter createAdapter(INodeNotifier target) {
if (org.eclipse.wst.sse.core.internal.util.Debug.displayInfo)
// $NON-NLS-1$
System.out.println("-----------------------ModelQueryAdapterFactoryForXML.createAdapter" + target);
if (modelQueryAdapterImpl == null) {
if (target instanceof IDOMNode) {
IDOMNode xmlNode = (IDOMNode) target;
IStructuredModel model = xmlNode.getModel();
stateNotifier = xmlNode.getModel();
stateNotifier.addModelStateListener(getInternalModelStateListener());
org.eclipse.wst.sse.core.internal.util.URIResolver resolver = model.getResolver();
if (Debug.displayInfo)
// $NON-NLS-1$
System.out.println("----------------ModelQueryAdapterFactoryForXML... baseLocation : " + resolver.getFileBaseLocation());
/**
* XMLCatalogIdResolver currently requires a filesystem
* location string. Customarily this will be what is in the
* deprecated SSE URIResolver and required by the Common URI
* Resolver.
*/
URIResolver idResolver = null;
if (resolver != null) {
idResolver = new XMLCatalogIdResolver(resolver.getFileBaseLocation(), resolver);
} else {
/*
* 203649 - this block may be necessary due to ordering of
* setting the resolver into the model
*/
String baseLocation = null;
String modelsBaseLocation = model.getBaseLocation();
if (modelsBaseLocation != null) {
File file = new Path(modelsBaseLocation).toFile();
if (file.exists()) {
baseLocation = file.getAbsolutePath();
} else {
IPath basePath = new Path(model.getBaseLocation());
IResource derivedResource = null;
if (basePath.segmentCount() > 1)
derivedResource = ResourcesPlugin.getWorkspace().getRoot().getFile(basePath);
else
derivedResource = ResourcesPlugin.getWorkspace().getRoot().getProject(basePath.segment(0));
IPath derivedPath = derivedResource.getLocation();
if (derivedPath != null) {
baseLocation = derivedPath.toString();
} else {
URI uri = derivedResource.getLocationURI();
if (uri != null) {
baseLocation = uri.toString();
}
}
}
if (baseLocation == null) {
baseLocation = modelsBaseLocation;
}
}
idResolver = new XMLCatalogIdResolver(baseLocation, null);
}
CMDocumentCache cmDocumentCache = new CMDocumentCache();
ModelQuery modelQuery = new XMLModelQueryImpl(cmDocumentCache, idResolver);
// cs todo...
// for now we create a CMDocumentCache on a 'per editor' basis
// in the future we need to support a CMDocumentCache that is
// shared between editors
// nsd comment: may not be appropriate depending on
CMDocumentManager documentManager = modelQuery.getCMDocumentManager();
if (documentManager != null) {
configureDocumentManager(documentManager);
}
modelQueryAdapterImpl = new ModelQueryAdapterImpl(cmDocumentCache, modelQuery, idResolver);
}
}
return modelQueryAdapterImpl;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class TestModelClone method getEmbeddedModelQuery.
private ModelQuery getEmbeddedModelQuery(Node node) {
ModelQuery embeddedModelQuery = null;
if (node instanceof INodeNotifier) {
Node ownerNode = node.getOwnerDocument();
if (ownerNode == null) {
// then must be the document itself
ownerNode = node;
}
PageDirectiveAdapter pageDirectiveAdapter = (PageDirectiveAdapter) ((INodeNotifier) ownerNode).getAdapterFor(PageDirectiveAdapter.class);
if (pageDirectiveAdapter != null) {
String effectiveContentType = null;
ModelQuery potentialModelQueryObject = null;
String familyId = pageDirectiveAdapter.getEmbeddedType().getFamilyId();
// if (ContentTypeFamilyForHTML.HTML_FAMILY.equals(familyId)) {
if ("org.eclipse.wst.html.core.contentfamily.html".equals(familyId)) {
effectiveContentType = "text/html";
} else {
effectiveContentType = pageDirectiveAdapter.getContentType();
}
potentialModelQueryObject = (ModelQuery) embeddedModelQueries.get(effectiveContentType);
if (potentialModelQueryObject == null) {
ModelQueryAdapter embeddedAdapter = (ModelQueryAdapter) pageDirectiveAdapter.adapt((INodeNotifier) node, ModelQueryAdapter.class);
if (embeddedAdapter != null) {
// we will cache one model query per content type
embeddedModelQuery = embeddedAdapter.getModelQuery();
embeddedModelQueries.put(effectiveContentType, embeddedModelQuery);
}
} else {
embeddedModelQuery = potentialModelQueryObject;
}
}
}
return embeddedModelQuery;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class XMLPropertySource method updatePropertyDescriptors.
protected void updatePropertyDescriptors() {
if ((fDescriptors == null) || (fDescriptors.length == 0)) {
// Nothing to update
return;
}
// List of all names encountered in the tag and defined by the element
List declaredNames = new ArrayList();
// New descriptor list that will become fDescriptors after all
// processing is done
List descriptors = new ArrayList();
// Names of the descriptors in the above List
List descriptorNames = new ArrayList();
// Update any descriptors derived from the metainfo
CMElementDeclaration ed = getDeclaration();
CMNamedNodeMap attrMap = null;
if (ed != null) {
attrMap = ed.getAttributes();
CMNamedNodeMapImpl allAttributes = new CMNamedNodeMapImpl(attrMap);
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(fNode.getOwnerDocument());
if (modelQuery != null) {
List nodes = modelQuery.getAvailableContent((Element) fNode, ed, ModelQuery.INCLUDE_ATTRIBUTES);
for (int k = 0; k < nodes.size(); k++) {
CMNode cmnode = (CMNode) nodes.get(k);
if (cmnode.getNodeType() == CMNode.ATTRIBUTE_DECLARATION) {
allAttributes.put(cmnode);
}
}
}
attrMap = allAttributes;
}
// Update exiting descriptors; not added to the final list here
if (attrMap != null) {
// Update existing descriptor types based on metainfo
CMAttributeDeclaration attrDecl = null;
for (int i = 0; i < attrMap.getLength(); i++) {
attrDecl = (CMAttributeDeclaration) attrMap.item(i);
String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
if (!declaredNames.contains(attrName)) {
declaredNames.add(attrName);
}
for (int j = 0; j < fDescriptors.length; j++) {
boolean sameName = (fCaseSensitive && fDescriptors[j].getId().equals(attrDecl.getNodeName())) || (!fCaseSensitive && attrDecl.getNodeName().equals(fDescriptors[j].getId().toString()));
if (sameName) {
String[] validValues = getValidValues(attrDecl);
// updated for now)
if (fDescriptors[j] instanceof EnumeratedStringPropertyDescriptor) {
((EnumeratedStringPropertyDescriptor) fDescriptors[j]).updateValues(validValues);
} else // Replace with better descriptor
if ((validValues != null) && (validValues.length > 0)) {
fDescriptors[j] = createPropertyDescriptor(attrDecl, null);
}
}
}
}
} else {
// Update existing descriptors based on not having any metainfo
for (int j = 0; j < fDescriptors.length; j++) {
// Replace with basic descriptor
if (!(fDescriptors[j] instanceof TextPropertyDescriptor)) {
fDescriptors[j] = createDefaultPropertyDescriptor((String) fDescriptors[j].getId());
}
}
}
NamedNodeMap attributes = fNode.getAttributes();
// are present or declared
for (int i = 0; i < fDescriptors.length; i++) {
if (fDescriptors[i] != null) {
String descriptorName = fDescriptors[i].getId().toString();
if ((declaredNames.contains(descriptorName) || (attributes.getNamedItem(descriptorName) != null)) && !descriptorNames.contains(descriptorName)) {
descriptorNames.add(descriptorName);
descriptors.add(fDescriptors[i]);
}
}
}
// Add descriptors for declared attributes that don't already have one
if (attrMap != null) {
// Update existing descriptor types based on metainfo
CMAttributeDeclaration attrDecl = null;
for (int i = 0; i < attrMap.getLength(); i++) {
attrDecl = (CMAttributeDeclaration) attrMap.item(i);
String attrName = DOMNamespaceHelper.computeName(attrDecl, fNode, null);
if (fCaseSensitive) {
if (!descriptorNames.contains(attrName)) {
IPropertyDescriptor descriptor = createPropertyDescriptor(attrDecl, null);
if (descriptor != null) {
descriptorNames.add(attrName);
descriptors.add(descriptor);
}
}
} else {
boolean exists = false;
for (int j = 0; j < descriptorNames.size(); j++) {
exists = (descriptorNames.get(j).toString().equalsIgnoreCase(attrName)) || exists;
}
if (!exists) {
descriptorNames.add(attrName);
IPropertyDescriptor descriptor = createPropertyDescriptor(attrDecl, null);
if (descriptor != null) {
descriptorNames.add(attrName);
descriptors.add(descriptor);
}
}
}
}
}
// Add descriptors for existing attributes that don't already have one
if (attributes != null) {
for (int i = 0; i < attributes.getLength(); i++) {
Attr attr = (Attr) attributes.item(i);
String attrName = attr.getName();
if (fCaseSensitive) {
if (!descriptorNames.contains(attrName)) {
descriptorNames.add(attrName);
descriptors.add(createDefaultPropertyDescriptor(attrName));
}
} else {
boolean exists = false;
for (int j = 0; j < descriptorNames.size(); j++) {
exists = (descriptorNames.get(j).toString().equalsIgnoreCase(attrName)) || exists;
}
if (!exists) {
descriptorNames.add(attrName);
descriptors.add(createDefaultPropertyDescriptor(attrName));
}
}
}
}
// Update fDescriptors
IPropertyDescriptor[] newDescriptors = new IPropertyDescriptor[descriptors.size()];
for (int i = 0; i < newDescriptors.length; i++) {
newDescriptors[i] = (IPropertyDescriptor) descriptors.get(i);
}
fDescriptors = newDescriptors;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class GlobalCMDocumentCacheTest method getCMDocumentFromXMLFile.
private CMDocument getCMDocumentFromXMLFile(String documentPath) {
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(documentPath));
IModelManager modelManager = StructuredModelManager.getModelManager();
IStructuredModel structuredModel = null;
CMDocument cmDocument = null;
try {
structuredModel = modelManager.getModelForRead(file);
Document document = ((IDOMModel) structuredModel).getDocument();
ModelQuery modelQuery = ModelQueryUtil.getModelQuery(document);
CMDocumentManager cmDocumentManager = modelQuery.getCMDocumentManager();
CMDocumentLoader loader = new CMDocumentLoader(document, cmDocumentManager);
loader.loadCMDocuments();
cmDocument = modelQuery.getCorrespondingCMDocument(document.getDocumentElement());
} catch (Exception exception) {
exception.printStackTrace();
} finally {
if (structuredModel != null) {
structuredModel.releaseFromRead();
}
}
return cmDocument;
}
use of org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery in project webtools.sourceediting by eclipse.
the class CMVisitorTest method test.
protected void test(String fileName) {
ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
try {
XMLAssociationProvider provider = new XMLAssociationProvider(new CMDocumentCache()) {
};
ModelQuery mq = new ModelQueryImpl(provider);
Thread.currentThread().setContextClassLoader(CMVisitorTest.class.getClassLoader());
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = builder.parse(fileName);
/*
* ClassLoader prevClassLoader =
* Thread.currentThread().getContextClassLoader();
* Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
* Class theClass =
* Class.forName("org.apache.xerces.parsers.DOMParser"); DOMParser
* parser = (DOMParser)theClass.newInstance();
* Thread.currentThread().setContextClassLoader(prevClassLoader);
* parser.parse(new InputSource(fileName)); Document document =
* parser.getDocument();
*/
visitNode(document, mq, 0);
} catch (Exception e) {
e.printStackTrace();
} finally {
Thread.currentThread().setContextClassLoader(originalClassLoader);
}
}
Aggregations