update copyright
[fedora-idea.git] / xml / dom-openapi / src / com / intellij / util / xml / Converter.java
blob48a02fc438474b20bb045e0f55cd551602727e1f
1 /*
2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.util.xml;
18 import com.intellij.codeInsight.CodeInsightBundle;
19 import com.intellij.ide.IdeBundle;
20 import org.jetbrains.annotations.NonNls;
21 import org.jetbrains.annotations.Nullable;
23 /**
24 * Base DOM class to convert objects of a definite type into {@link String} and back. Most often used with
25 * {@link com.intellij.util.xml.Convert} annotation with methods returning {@link com.intellij.util.xml.GenericDomValue}<T>.
27 * @see com.intellij.util.xml.ResolvingConverter
28 * @see com.intellij.util.xml.CustomReferenceConverter
30 * @param <T> Type to convert from/to.
32 * @author peter
34 public abstract class Converter<T> {
35 @Nullable
36 public abstract T fromString(@Nullable @NonNls String s, final ConvertContext context);
37 @Nullable
38 public abstract String toString(@Nullable T t, final ConvertContext context);
40 /**
41 * @param s string value that couldn't be resolved
42 * @param context context
43 * @return error message used to highlight the errors somewhere in the UI, most often - like unresolved references in XML
45 @Nullable
46 public String getErrorMessage(@Nullable String s, final ConvertContext context) {
47 return CodeInsightBundle.message("error.cannot.convert.default.message", s);
51 /**
52 * @deprecated {@link com.intellij.util.xml.converters.values.NumberValueConverter}
54 @Deprecated
55 public static final Converter<Integer> INTEGER_CONVERTER = new Converter<Integer>() {
56 public Integer fromString(final String s, final ConvertContext context) {
57 if (s == null) return null;
58 try {
59 return Integer.decode(s);
61 catch (Exception e) {
62 return null;
66 public String toString(final Integer t, final ConvertContext context) {
67 return t == null? null: t.toString();
70 public String getErrorMessage(final String s, final ConvertContext context) {
71 return IdeBundle.message("value.should.be.integer");
75 @Deprecated
76 public static final Converter<String> EMPTY_CONVERTER = new Converter<String>() {
77 public String fromString(final String s, final ConvertContext context) {
78 return s;
81 public String toString(final String t, final ConvertContext context) {
82 return t;