Search in sources :

Example 1 with ICSSNodeList

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList in project webtools.sourceediting by eclipse.

the class CSSPropertySource method getPropertyDescriptors.

/**
 * Returns the current collection of property descriptors.
 *
 * @return a vector containing all descriptors.
 */
public IPropertyDescriptor[] getPropertyDescriptors() {
    time0 = System.currentTimeMillis();
    CSSMetaModel metamodel = CSSMetaModelFinder.getInstance().findMetaModelFor(fNode);
    Iterator iProperties = Collections.EMPTY_LIST.iterator();
    switch(fNode.getNodeType()) {
        case ICSSNode.STYLERULE_NODE:
        case ICSSNode.FONTFACERULE_NODE:
        case ICSSNode.PAGERULE_NODE:
        case ICSSNode.STYLEDECLARATION_NODE:
            CSSMMNode mmParent = new CSSMetaModelUtil(metamodel).getMetaModelNodeFor(fNode);
            if (mmParent != null) {
                iProperties = mmParent.getChildNodes();
            }
            break;
        case ICSSNode.STYLEDECLITEM_NODE:
            CSSMMNode mmNode = new CSSMetaModelUtil(metamodel).getMetaModelNodeFor(fNode);
            if (mmNode != null) {
                iProperties = Collections.singletonList(mmNode).iterator();
            }
            break;
        default:
            break;
    }
    // setup categories
    Map categories = new HashMap();
    Iterator iCategories = metamodel.getCategories();
    while (iCategories.hasNext()) {
        CSSMMCategory category = (CSSMMCategory) iCategories.next();
        categories.put(category.getName(), category.getCaption());
    }
    // collect property names
    Set declaredProperties = new HashSet();
    if (iProperties.hasNext()) {
        CSSStyleDeclaration declaration = getDeclarationNode();
        if (declaration != null) {
            ICSSNodeList nodeList = ((ICSSNode) declaration).getChildNodes();
            int nProps = (nodeList != null) ? nodeList.getLength() : 0;
            for (int i = 0; i < nProps; i++) {
                ICSSNode node = nodeList.item(i);
                if (node instanceof ICSSStyleDeclItem) {
                    String name = ((ICSSStyleDeclItem) node).getPropertyName();
                    if (name != null && 0 < name.length()) {
                        declaredProperties.add(name.toLowerCase());
                    }
                }
            }
        }
    }
    List descriptors = new ArrayList();
    // first: properties from content model
    while (iProperties.hasNext()) {
        CSSMMNode node = (CSSMMNode) iProperties.next();
        if (node.getType() == CSSMMNode.TYPE_PROPERTY || node.getType() == CSSMMNode.TYPE_DESCRIPTOR) {
            // $NON-NLS-1$
            String category = (String) categories.get(node.getAttribute("category"));
            String name = node.getName().toLowerCase();
            if (declaredProperties.contains(name)) {
                declaredProperties.remove(name);
            }
            IPropertyDescriptor descriptor = createPropertyDescriptor(name, category);
            if (descriptor != null) {
                descriptors.add(descriptor);
            }
        }
    }
    // second: existing properties but not in content model
    Iterator iRemains = declaredProperties.iterator();
    while (iRemains.hasNext()) {
        IPropertyDescriptor descriptor = createPropertyDescriptor((String) iRemains.next(), null);
        if (descriptor != null) {
            descriptors.add(descriptor);
        }
    }
    IPropertyDescriptor[] resultArray = new IPropertyDescriptor[descriptors.size()];
    if (PERF_GETDESCRIPTORS) {
        // $NON-NLS-1$ //$NON-NLS-2$
        System.out.println(getClass().getName() + ".getPropertyDescriptors: " + (System.currentTimeMillis() - time0) + "ms");
    }
    return (IPropertyDescriptor[]) descriptors.toArray(resultArray);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) CSSMetaModelUtil(org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil) HashMap(java.util.HashMap) CSSMetaModel(org.eclipse.wst.css.core.internal.metamodel.CSSMetaModel) ArrayList(java.util.ArrayList) CSSStyleDeclaration(org.w3c.dom.css.CSSStyleDeclaration) ICSSNode(org.eclipse.wst.css.core.internal.provisional.document.ICSSNode) ICSSNodeList(org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList) CSSMMNode(org.eclipse.wst.css.core.internal.metamodel.CSSMMNode) CSSMMCategory(org.eclipse.wst.css.core.internal.metamodel.CSSMMCategory) ICSSStyleDeclItem(org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem) Iterator(java.util.Iterator) ICSSNodeList(org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList) ArrayList(java.util.ArrayList) List(java.util.List) IPropertyDescriptor(org.eclipse.ui.views.properties.IPropertyDescriptor) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 2 with ICSSNodeList

use of org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList in project webtools.sourceediting by eclipse.

the class CSSModelUtil method diagnoseNode.

static boolean diagnoseNode(ICSSNode parent, IStructuredDocument structuredDocument) {
    // check this
    Vector errors = new Vector();
    if (parent instanceof CSSStructuredDocumentRegionContainer) {
        CSSStructuredDocumentRegionContainer node = (CSSStructuredDocumentRegionContainer) parent;
        // $NON-NLS-1$
        String nodeText = CSSUtil.getClassString(node) + ": ";
        IStructuredDocumentRegion flatNode = node.getFirstStructuredDocumentRegion();
        if (flatNode == null && (!(node instanceof CSSStyleDeclarationImpl || node instanceof CSSStyleSheetImpl) || node.getFirstChild() != null)) {
            // $NON-NLS-1$
            errors.add(nodeText + "first flat node is null.");
        } else if (flatNode != null) {
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(flatNode.getStart());
            if (flatNode != modelNode) {
                // $NON-NLS-1$
                errors.add(nodeText + "first flat node is not in model.");
            }
        }
        flatNode = node.getLastStructuredDocumentRegion();
        if (flatNode == null && (!(node instanceof CSSStyleDeclarationImpl || node instanceof CSSStyleSheetImpl) || node.getFirstChild() != null)) {
            // $NON-NLS-1$
            errors.add(nodeText + "last flat node is null.");
        } else if (flatNode != null) {
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(flatNode.getStart());
            if (flatNode != modelNode) {
                // $NON-NLS-1$
                errors.add(nodeText + "last flat node is not in model.");
            }
        }
    } else if (parent instanceof CSSRegionContainer) {
        CSSRegionContainer node = (CSSRegionContainer) parent;
        // $NON-NLS-1$
        String nodeText = CSSUtil.getClassString(node) + ": ";
        ITextRegion region = node.getFirstRegion();
        IStructuredDocumentRegion parentRegion = node.getDocumentRegion();
        if (region == null && (!(node instanceof MediaListImpl) || node.getFirstChild() != null)) {
            // $NON-NLS-1$
            errors.add(nodeText + "first region is null.");
        } else if (region != null) {
            int offset = parentRegion.getStartOffset(region);
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
            ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
            if (region != modelRegion) {
                // $NON-NLS-1$
                errors.add(nodeText + "first region is not in model.");
            }
        }
        region = node.getLastRegion();
        if (region == null && (!(node instanceof MediaListImpl) || node.getFirstChild() != null)) {
            // $NON-NLS-1$
            errors.add(nodeText + "last region is null.");
        } else if (region != null) {
            int offset = parentRegion.getStartOffset(region);
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
            ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
            if (region != modelRegion) {
                // $NON-NLS-1$
                errors.add(nodeText + "last region is not in model.");
            }
        }
    }
    ICSSNodeList attrs = parent.getAttributes();
    int nAttrs = attrs.getLength();
    for (int i = 0; i < nAttrs; i++) {
        ICSSAttr attr = (ICSSAttr) attrs.item(i);
        CSSRegionContainer node = (CSSRegionContainer) attr;
        // $NON-NLS-2$//$NON-NLS-1$
        String nodeText = CSSUtil.getClassString(node) + "(" + attr.getName() + "): ";
        ITextRegion region = node.getFirstRegion();
        IStructuredDocumentRegion parentRegion = node.getDocumentRegion();
        if (region == null && 0 < attr.getValue().length()) {
            // $NON-NLS-1$
            errors.add(nodeText + "first region is null.");
        } else if (region != null) {
            int offset = parentRegion.getStartOffset(region);
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
            ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
            if (region != modelRegion) {
                // $NON-NLS-1$
                errors.add(nodeText + "first region is not in model.");
            }
        }
        region = node.getLastRegion();
        if (region == null && 0 < attr.getValue().length()) {
            // $NON-NLS-1$
            errors.add(nodeText + "last region is null.");
        } else if (region != null) {
            int offset = parentRegion.getStartOffset(region);
            IStructuredDocumentRegion modelNode = structuredDocument.getRegionAtCharacterOffset(offset);
            ITextRegion modelRegion = modelNode.getRegionAtCharacterOffset(offset);
            if (region != modelRegion) {
                // $NON-NLS-1$
                errors.add(nodeText + "last region is not in model.");
            }
        }
    }
    if (!errors.isEmpty()) {
        Iterator i = errors.iterator();
        while (i.hasNext()) {
            CSSUtil.debugOut((String) i.next());
        }
        return false;
    } else {
        return true;
    }
}
Also used : ICSSAttr(org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr) IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) ICSSNodeList(org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) Iterator(java.util.Iterator) Vector(java.util.Vector)

Aggregations

Iterator (java.util.Iterator)2 ICSSNodeList (org.eclipse.wst.css.core.internal.provisional.document.ICSSNodeList)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Vector (java.util.Vector)1 IPropertyDescriptor (org.eclipse.ui.views.properties.IPropertyDescriptor)1 CSSMMCategory (org.eclipse.wst.css.core.internal.metamodel.CSSMMCategory)1 CSSMMNode (org.eclipse.wst.css.core.internal.metamodel.CSSMMNode)1 CSSMetaModel (org.eclipse.wst.css.core.internal.metamodel.CSSMetaModel)1 CSSMetaModelUtil (org.eclipse.wst.css.core.internal.metamodel.util.CSSMetaModelUtil)1 ICSSAttr (org.eclipse.wst.css.core.internal.provisional.document.ICSSAttr)1 ICSSNode (org.eclipse.wst.css.core.internal.provisional.document.ICSSNode)1 ICSSStyleDeclItem (org.eclipse.wst.css.core.internal.provisional.document.ICSSStyleDeclItem)1 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)1 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)1 CSSStyleDeclaration (org.w3c.dom.css.CSSStyleDeclaration)1