• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 The Android Open Source Project
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package com.android.vcard;
17 
18 import java.util.Set;
19 
20 
21 /**
22  * <p>
23  * Basic implementation parsing vCard 4.0.
24  * </p>
25  * <p>
26  * vCard 4.0 is not published yet. Also this implementation is premature.
27  * </p>
28  * @hide
29  */
30 /* package */ class VCardParserImpl_V40 extends VCardParserImpl_V30 {
31     // private static final String LOG_TAG = VCardConstants.LOG_TAG;
32 
VCardParserImpl_V40()33     public VCardParserImpl_V40() {
34         super();
35     }
36 
VCardParserImpl_V40(final int vcardType)37     public VCardParserImpl_V40(final int vcardType) {
38         super(vcardType);
39     }
40 
41     @Override
getVersion()42     protected int getVersion() {
43         return VCardConfig.VERSION_40;
44     }
45 
46     @Override
getVersionString()47     protected String getVersionString() {
48         return VCardConstants.VERSION_V40;
49     }
50 
51     /**
52      * We escape "\N" into new line for safety.
53      */
54     @Override
maybeUnescapeText(final String text)55     protected String maybeUnescapeText(final String text) {
56         return unescapeText(text);
57     }
58 
unescapeText(final String text)59     public static String unescapeText(final String text) {
60         // TODO: more strictly, vCard 4.0 requires different type of unescaping rule
61         //       toward each property.
62         final StringBuilder builder = new StringBuilder();
63         final int length = text.length();
64         for (int i = 0; i < length; i++) {
65             char ch = text.charAt(i);
66             if (ch == '\\' && i < length - 1) {
67                 final char next_ch = text.charAt(++i);
68                 if (next_ch == 'n' || next_ch == 'N') {
69                     builder.append("\n");
70                 } else {
71                     builder.append(next_ch);
72                 }
73             } else {
74                 builder.append(ch);
75             }
76         }
77         return builder.toString();
78     }
79 
unescapeCharacter(final char ch)80     public static String unescapeCharacter(final char ch) {
81         if (ch == 'n' || ch == 'N') {
82             return "\n";
83         } else {
84             return String.valueOf(ch);
85         }
86     }
87 
88     @Override
getKnownPropertyNameSet()89     protected Set<String> getKnownPropertyNameSet() {
90         return VCardParser_V40.sKnownPropertyNameSet;
91     }
92 }