Search in sources :

Example 1 with DomInvocationHandler

use of com.intellij.util.xml.impl.DomInvocationHandler in project intellij-community by JetBrains.

the class DomAttributeXmlDescriptor method getQualifiedAttributeName.

static String getQualifiedAttributeName(PsiElement context, XmlName xmlName) {
    final String localName = xmlName.getLocalName();
    if (context instanceof XmlTag) {
        final XmlTag tag = (XmlTag) context;
        final DomInvocationHandler handler = DomManagerImpl.getDomManager(context.getProject()).getDomHandler(tag);
        if (handler != null) {
            final String ns = handler.createEvaluatedXmlName(xmlName).getNamespace(tag, handler.getFile());
            if (!ns.equals(XmlUtil.EMPTY_URI) && !ns.equals(tag.getNamespace())) {
                final String prefix = tag.getPrefixByNamespace(ns);
                if (StringUtil.isNotEmpty(prefix)) {
                    return prefix + ":" + localName;
                }
            }
        }
    }
    return localName;
}
Also used : DomInvocationHandler(com.intellij.util.xml.impl.DomInvocationHandler) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with DomInvocationHandler

use of com.intellij.util.xml.impl.DomInvocationHandler in project android by JetBrains.

the class DimensionConverterTest method test.

public void test() {
    DimensionConverter converter = new DimensionConverter();
    StyleItem element = createElement("<item>10dp</item>", StyleItem.class);
    DomInvocationHandler handler = DomManagerImpl.getDomInvocationHandler(element);
    assertNotNull(handler);
    ConvertContext context = ConvertContextFactory.createConvertContext(handler);
    List<String> variants = new ArrayList<>(converter.getVariants(context));
    Collections.sort(variants);
    assertEquals(Arrays.asList("10dp", "10in", "10mm", "10pt", "10px", "10sp"), variants);
    // Valid units
    assertEquals("1dip", converter.fromString("1dip", context));
    assertEquals("1dp", converter.fromString("1dp", context));
    assertEquals("1px", converter.fromString("1px", context));
    assertEquals("1in", converter.fromString("1in", context));
    assertEquals("1mm", converter.fromString("1mm", context));
    assertEquals("1sp", converter.fromString("1sp", context));
    assertEquals("1pt", converter.fromString("1pt", context));
    // Invalid dimensions (missing units)
    assertNull(converter.fromString("not_a_dimension", context));
    assertNull(converter.fromString("", context));
    assertEquals("Cannot resolve symbol ''", converter.getErrorMessage("", context));
    assertNull(converter.fromString("1", context));
    assertEquals("Cannot resolve symbol '1'", converter.getErrorMessage("1", context));
    assertNull(converter.fromString("1.5", context));
    assertEquals("Cannot resolve symbol '1.5'", converter.getErrorMessage("1.5", context));
    // Unknown units
    assertNull(converter.fromString("15d", context));
    assertEquals("Unknown unit 'd'", converter.getErrorMessage("15d", context));
    assertNull(converter.fromString("15wrong", context));
    assertEquals("Unknown unit 'wrong'", converter.getErrorMessage("15wrong", context));
    // Normal conversions
    assertEquals("15px", converter.fromString("15px", context));
    assertEquals("15", DimensionConverter.getIntegerPrefix("15px"));
    assertEquals("px", DimensionConverter.getUnitFromValue("15px"));
    // Make sure negative numbers work
    assertEquals("-10px", converter.fromString("-10px", context));
    assertEquals("-10", DimensionConverter.getIntegerPrefix("-10px"));
    assertEquals("px", DimensionConverter.getUnitFromValue("-10px"));
    // Make sure decimals work
    assertEquals("1.5sp", converter.fromString("1.5sp", context));
    assertEquals("1.5", DimensionConverter.getIntegerPrefix("1.5sp"));
    assertEquals("sp", DimensionConverter.getUnitFromValue("1.5sp"));
    assertEquals(".5sp", converter.fromString(".5sp", context));
    assertEquals(".5", DimensionConverter.getIntegerPrefix(".5sp"));
    assertEquals("sp", DimensionConverter.getUnitFromValue(".5sp"));
    // Make sure the right type of decimal separator is used
    assertNull(converter.fromString("1,5sp", context));
    assertEquals("Use a dot instead of a comma as the decimal mark", converter.getErrorMessage("1,5sp", context));
    // Documentation
    assertEquals("<html><body>" + "<b>Density-independent Pixels</b> - an abstract unit that is based on the physical density of the screen." + "</body></html>", converter.getDocumentation("1dp"));
    assertEquals("<html><body>" + "<b>Pixels</b> - corresponds to actual pixels on the screen. Not recommended." + "</body></html>", converter.getDocumentation("-10px"));
    assertEquals("<html><body>" + "<b>Scale-independent Pixels</b> - this is like the dp unit, but " + "it is also scaled by the user's font size preference." + "</body></html>", converter.getDocumentation("1.5sp"));
}
Also used : StyleItem(org.jetbrains.android.dom.resources.StyleItem) ArrayList(java.util.ArrayList) DomInvocationHandler(com.intellij.util.xml.impl.DomInvocationHandler) ConvertContext(com.intellij.util.xml.ConvertContext)

Example 3 with DomInvocationHandler

use of com.intellij.util.xml.impl.DomInvocationHandler in project intellij-community by JetBrains.

the class DomStubBuilderVisitor method visitXmlElement.

void visitXmlElement(XmlElement element, ElementStub parent, int index) {
    DomInvocationHandler handler = myManager.getDomHandler(element);
    if (handler == null || handler.getAnnotation(Stubbed.class) == null && !handler.getChildDescription().isStubbed())
        return;
    AbstractDomChildrenDescription description = handler.getChildDescription();
    String nsKey = description instanceof DomChildrenDescription ? ((DomChildrenDescription) description).getXmlName().getNamespaceKey() : "";
    if (element instanceof XmlTag) {
        XmlTag tag = (XmlTag) element;
        String elementClass = null;
        if (handler.getAnnotation(StubbedOccurrence.class) != null) {
            final Type type = description.getType();
            elementClass = ((Class) type).getName();
        }
        ElementStub stub = new ElementStub(parent, StringRef.fromString(tag.getName()), StringRef.fromNullableString(nsKey), index, description instanceof CustomDomChildrenDescription, elementClass == null ? null : StringRef.fromNullableString(elementClass), tag.getSubTags().length == 0 ? tag.getValue().getTrimmedText() : "");
        for (XmlAttribute attribute : tag.getAttributes()) {
            visitXmlElement(attribute, stub, 0);
        }
        Map<String, Integer> indices = new HashMap<>();
        for (final XmlTag subTag : tag.getSubTags()) {
            String name = subTag.getName();
            Integer i = indices.get(name);
            i = i == null ? 0 : i + 1;
            visitXmlElement(subTag, stub, i);
            indices.put(name, i);
        }
    } else if (element instanceof XmlAttribute) {
        new AttributeStub(parent, StringRef.fromString(((XmlAttribute) element).getLocalName()), StringRef.fromNullableString(nsKey), ((XmlAttribute) element).getValue());
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) Stubbed(com.intellij.util.xml.Stubbed) HashMap(java.util.HashMap) DomInvocationHandler(com.intellij.util.xml.impl.DomInvocationHandler) CustomDomChildrenDescription(com.intellij.util.xml.reflect.CustomDomChildrenDescription) DomChildrenDescription(com.intellij.util.xml.reflect.DomChildrenDescription) AbstractDomChildrenDescription(com.intellij.util.xml.reflect.AbstractDomChildrenDescription) CustomDomChildrenDescription(com.intellij.util.xml.reflect.CustomDomChildrenDescription) Type(java.lang.reflect.Type) AttributeStub(com.intellij.util.xml.stubs.AttributeStub) AbstractDomChildrenDescription(com.intellij.util.xml.reflect.AbstractDomChildrenDescription) ElementStub(com.intellij.util.xml.stubs.ElementStub) XmlTag(com.intellij.psi.xml.XmlTag) StubbedOccurrence(com.intellij.util.xml.StubbedOccurrence)

Aggregations

DomInvocationHandler (com.intellij.util.xml.impl.DomInvocationHandler)3 XmlTag (com.intellij.psi.xml.XmlTag)2 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 ConvertContext (com.intellij.util.xml.ConvertContext)1 Stubbed (com.intellij.util.xml.Stubbed)1 StubbedOccurrence (com.intellij.util.xml.StubbedOccurrence)1 AbstractDomChildrenDescription (com.intellij.util.xml.reflect.AbstractDomChildrenDescription)1 CustomDomChildrenDescription (com.intellij.util.xml.reflect.CustomDomChildrenDescription)1 DomChildrenDescription (com.intellij.util.xml.reflect.DomChildrenDescription)1 AttributeStub (com.intellij.util.xml.stubs.AttributeStub)1 ElementStub (com.intellij.util.xml.stubs.ElementStub)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 StyleItem (org.jetbrains.android.dom.resources.StyleItem)1