Search in sources :

Example 61 with ModelQuery

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;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) CMDocumentManager(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.CMDocumentManager) URIResolver(org.eclipse.wst.common.uriresolver.internal.provisional.URIResolver) ModelQueryAdapterImpl(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapterImpl) URI(java.net.URI) CMDocumentCache(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) File(java.io.File) IFile(org.eclipse.core.resources.IFile) IResource(org.eclipse.core.resources.IResource)

Example 62 with ModelQuery

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;
}
Also used : ModelQueryAdapter(org.eclipse.wst.xml.core.internal.ssemodelquery.ModelQueryAdapter) Node(org.w3c.dom.Node) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) PageDirectiveAdapter(org.eclipse.jst.jsp.core.internal.document.PageDirectiveAdapter) INodeNotifier(org.eclipse.wst.sse.core.internal.provisional.INodeNotifier)

Example 63 with ModelQuery

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;
}
Also used : CMNamedNodeMapImpl(org.eclipse.wst.xml.core.internal.contentmodel.basic.CMNamedNodeMapImpl) NamedNodeMap(org.w3c.dom.NamedNodeMap) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap) ArrayList(java.util.ArrayList) Attr(org.w3c.dom.Attr) CMElementDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) CMNode(org.eclipse.wst.xml.core.internal.contentmodel.CMNode) CMAttributeDeclaration(org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration) TextPropertyDescriptor(org.eclipse.ui.views.properties.TextPropertyDescriptor) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) CMNamedNodeMap(org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)

Example 64 with ModelQuery

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;
}
Also used : Path(org.eclipse.core.runtime.Path) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) IFile(org.eclipse.core.resources.IFile) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) CMDocumentManager(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.CMDocumentManager) IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) CMDocumentLoader(org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.CMDocumentLoader) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) CMDocument(org.eclipse.wst.xml.core.internal.contentmodel.CMDocument) Document(org.w3c.dom.Document)

Example 65 with ModelQuery

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);
    }
}
Also used : ModelQueryImpl(org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.ModelQueryImpl) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ModelQuery(org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery) XMLAssociationProvider(org.eclipse.wst.xml.core.internal.contentmodel.modelqueryimpl.XMLAssociationProvider) Document(org.w3c.dom.Document) CMDocumentCache(org.eclipse.wst.xml.core.internal.contentmodel.util.CMDocumentCache)

Aggregations

ModelQuery (org.eclipse.wst.xml.core.internal.contentmodel.modelquery.ModelQuery)76 CMElementDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMElementDeclaration)38 List (java.util.List)22 ArrayList (java.util.ArrayList)19 CMNode (org.eclipse.wst.xml.core.internal.contentmodel.CMNode)18 CMNamedNodeMap (org.eclipse.wst.xml.core.internal.contentmodel.CMNamedNodeMap)16 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)15 CMDocument (org.eclipse.wst.xml.core.internal.contentmodel.CMDocument)15 Element (org.w3c.dom.Element)15 Document (org.w3c.dom.Document)14 ITextRegionList (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionList)13 Node (org.w3c.dom.Node)12 Iterator (java.util.Iterator)10 CMAttributeDeclaration (org.eclipse.wst.xml.core.internal.contentmodel.CMAttributeDeclaration)10 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)10 IDOMNode (org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode)10 NodeList (org.w3c.dom.NodeList)10 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)7 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)7 IFile (org.eclipse.core.resources.IFile)6