Search in sources :

Example 1 with ConvertContext

use of com.intellij.util.xml.ConvertContext in project intellij-plugins by JetBrains.

the class InterceptorRefResolveConverterImpl method fromString.

public InterceptorOrStackBase fromString(@Nullable @NonNls final String name, final ConvertContext context) {
    if (name == null) {
        return null;
    }
    final Condition<InterceptorOrStackBase> nameCondition = interceptorOrStackBase -> name.equals(interceptorOrStackBase.getName().getStringValue());
    final Ref<InterceptorOrStackBase> resolveResult = new Ref<>();
    final Processor<StrutsPackage> processor = strutsPackage -> {
        final InterceptorOrStackBase result = ContainerUtil.find(getAllInterceptors(strutsPackage), nameCondition);
        if (result != null) {
            resolveResult.set(result);
            return false;
        }
        return true;
    };
    final StrutsPackageHierarchyWalker walker = new StrutsPackageHierarchyWalker(ConverterUtil.getCurrentStrutsPackage(context), processor);
    walker.walkUp();
    return resolveResult.get();
}
Also used : Collection(java.util.Collection) NonNls(org.jetbrains.annotations.NonNls) InterceptorOrStackBase(com.intellij.struts2.dom.struts.strutspackage.InterceptorOrStackBase) ContainerUtil(com.intellij.util.containers.ContainerUtil) StrutsPackageHierarchyWalker(com.intellij.struts2.dom.struts.strutspackage.StrutsPackageHierarchyWalker) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) SmartList(com.intellij.util.SmartList) Processor(com.intellij.util.Processor) StrutsPackage(com.intellij.struts2.dom.struts.strutspackage.StrutsPackage) ConverterUtil(com.intellij.struts2.dom.ConverterUtil) InterceptorRefResolveConverter(com.intellij.struts2.dom.struts.strutspackage.InterceptorRefResolveConverter) ConvertContext(com.intellij.util.xml.ConvertContext) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) Condition(com.intellij.openapi.util.Condition) StrutsPackageHierarchyWalker(com.intellij.struts2.dom.struts.strutspackage.StrutsPackageHierarchyWalker) Ref(com.intellij.openapi.util.Ref) InterceptorOrStackBase(com.intellij.struts2.dom.struts.strutspackage.InterceptorOrStackBase) StrutsPackage(com.intellij.struts2.dom.struts.strutspackage.StrutsPackage)

Example 2 with ConvertContext

use of com.intellij.util.xml.ConvertContext in project intellij-plugins by JetBrains.

the class ResultTypeResolvingConverterImpl method fromString.

public ResultType fromString(@Nullable @NonNls final String name, final ConvertContext context) {
    if (StringUtil.isEmpty(name)) {
        return null;
    }
    final Condition<ResultType> nameCondition = resultType -> Comparing.equal(name, resultType.getName().getStringValue());
    final Ref<ResultType> resolveResult = new Ref<>();
    final Processor<StrutsPackage> processor = strutsPackage -> {
        final ResultType result = ContainerUtil.find(strutsPackage.getResultTypes(), nameCondition);
        if (result != null) {
            resolveResult.set(result);
            return false;
        }
        return true;
    };
    final StrutsPackageHierarchyWalker walker = new StrutsPackageHierarchyWalker(ConverterUtil.getCurrentStrutsPackage(context), processor);
    walker.walkUp();
    return resolveResult.get();
}
Also used : StringUtil(com.intellij.openapi.util.text.StringUtil) Collection(java.util.Collection) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) StrutsPackageHierarchyWalker(com.intellij.struts2.dom.struts.strutspackage.StrutsPackageHierarchyWalker) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Comparing(com.intellij.openapi.util.Comparing) SmartList(com.intellij.util.SmartList) ResultType(com.intellij.struts2.dom.struts.strutspackage.ResultType) Processor(com.intellij.util.Processor) StrutsPackage(com.intellij.struts2.dom.struts.strutspackage.StrutsPackage) ConverterUtil(com.intellij.struts2.dom.ConverterUtil) ResultTypeResolvingConverter(com.intellij.struts2.dom.struts.action.ResultTypeResolvingConverter) ConvertContext(com.intellij.util.xml.ConvertContext) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) Condition(com.intellij.openapi.util.Condition) StrutsPackageHierarchyWalker(com.intellij.struts2.dom.struts.strutspackage.StrutsPackageHierarchyWalker) Ref(com.intellij.openapi.util.Ref) StrutsPackage(com.intellij.struts2.dom.struts.strutspackage.StrutsPackage) ResultType(com.intellij.struts2.dom.struts.strutspackage.ResultType)

Example 3 with ConvertContext

use of com.intellij.util.xml.ConvertContext in project intellij-plugins by JetBrains.

the class DefaultInterceptorRefResolveConverterImpl method fromString.

public InterceptorStack fromString(@Nullable @NonNls final String name, final ConvertContext context) {
    if (name == null) {
        return null;
    }
    final Condition<InterceptorStack> nameCondition = interceptorStack -> name.equals(interceptorStack.getName().getStringValue());
    final Ref<InterceptorStack> resolveResult = new Ref<>();
    final Processor<StrutsPackage> processor = strutsPackage -> {
        final InterceptorStack result = ContainerUtil.find(strutsPackage.getInterceptorStacks(), nameCondition);
        if (result != null) {
            resolveResult.set(result);
            return false;
        }
        return true;
    };
    final StrutsPackageHierarchyWalker walker = new StrutsPackageHierarchyWalker(getCurrentStrutsPackage(context), processor);
    walker.walkUp();
    return resolveResult.get();
}
Also used : DefaultInterceptorRefResolveConverter(com.intellij.struts2.dom.struts.strutspackage.DefaultInterceptorRefResolveConverter) Collection(java.util.Collection) NonNls(org.jetbrains.annotations.NonNls) ContainerUtil(com.intellij.util.containers.ContainerUtil) StrutsPackageHierarchyWalker(com.intellij.struts2.dom.struts.strutspackage.StrutsPackageHierarchyWalker) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) SmartList(com.intellij.util.SmartList) Processor(com.intellij.util.Processor) StrutsPackage(com.intellij.struts2.dom.struts.strutspackage.StrutsPackage) InterceptorStack(com.intellij.struts2.dom.struts.strutspackage.InterceptorStack) ConvertContext(com.intellij.util.xml.ConvertContext) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) DomUtil(com.intellij.util.xml.DomUtil) Condition(com.intellij.openapi.util.Condition) StrutsPackageHierarchyWalker(com.intellij.struts2.dom.struts.strutspackage.StrutsPackageHierarchyWalker) Ref(com.intellij.openapi.util.Ref) InterceptorStack(com.intellij.struts2.dom.struts.strutspackage.InterceptorStack) StrutsPackage(com.intellij.struts2.dom.struts.strutspackage.StrutsPackage)

Example 4 with ConvertContext

use of com.intellij.util.xml.ConvertContext 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 5 with ConvertContext

use of com.intellij.util.xml.ConvertContext in project intellij-plugins by JetBrains.

the class StrutsConstantManagerImpl method getConvertedValue.

@Override
@Nullable
public <T> T getConvertedValue(@NotNull final PsiElement context, @NotNull final StrutsConstantKey<T> strutsConstantKey) {
    final PsiFile containingFile = context.getContainingFile();
    final StrutsModel strutsModel = getStrutsModel(containingFile);
    if (strutsModel == null) {
        return null;
    }
    final String stringValue = getStringValue(containingFile, strutsModel, strutsConstantKey.getKey());
    if (stringValue == null) {
        return null;
    }
    final Converter<T> converter = findConverter(context, strutsConstantKey);
    if (converter == null) {
        //noinspection unchecked
        return (T) stringValue;
    }
    final DomFileElement<StrutsRoot> first = strutsModel.getRoots().iterator().next();
    final ConvertContext convertContext = ConvertContextFactory.createConvertContext(first);
    //noinspection unchecked
    return converter.fromString(stringValue, convertContext);
}
Also used : StrutsModel(com.intellij.struts2.dom.struts.model.StrutsModel) PsiFile(com.intellij.psi.PsiFile) ConvertContext(com.intellij.util.xml.ConvertContext) StrutsRoot(com.intellij.struts2.dom.struts.StrutsRoot) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

ConvertContext (com.intellij.util.xml.ConvertContext)8 Nullable (org.jetbrains.annotations.Nullable)7 Condition (com.intellij.openapi.util.Condition)6 Ref (com.intellij.openapi.util.Ref)6 StrutsPackage (com.intellij.struts2.dom.struts.strutspackage.StrutsPackage)6 StrutsPackageHierarchyWalker (com.intellij.struts2.dom.struts.strutspackage.StrutsPackageHierarchyWalker)6 Processor (com.intellij.util.Processor)6 SmartList (com.intellij.util.SmartList)6 ContainerUtil (com.intellij.util.containers.ContainerUtil)6 Collection (java.util.Collection)6 List (java.util.List)6 NonNls (org.jetbrains.annotations.NonNls)6 NotNull (org.jetbrains.annotations.NotNull)6 ConverterUtil (com.intellij.struts2.dom.ConverterUtil)4 Comparing (com.intellij.openapi.util.Comparing)2 StringUtil (com.intellij.openapi.util.text.StringUtil)2 ResultTypeResolvingConverter (com.intellij.struts2.dom.struts.action.ResultTypeResolvingConverter)2 DefaultInterceptorRefResolveConverter (com.intellij.struts2.dom.struts.strutspackage.DefaultInterceptorRefResolveConverter)2 InterceptorOrStackBase (com.intellij.struts2.dom.struts.strutspackage.InterceptorOrStackBase)2 InterceptorRefResolveConverter (com.intellij.struts2.dom.struts.strutspackage.InterceptorRefResolveConverter)2