1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php 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 17 package com.android.ide.eclipse.adt.internal.editors.layout; 18 19 import static com.android.SdkConstants.ATTR_IGNORE; 20 import static com.android.SdkConstants.ATTR_LAYOUT; 21 import static com.android.SdkConstants.ATTR_LAYOUT_HEIGHT; 22 import static com.android.SdkConstants.ATTR_LAYOUT_WIDTH; 23 import static com.android.SdkConstants.EXPANDABLE_LIST_VIEW; 24 import static com.android.SdkConstants.GRID_VIEW; 25 import static com.android.SdkConstants.LIST_VIEW; 26 import static com.android.SdkConstants.SPINNER; 27 import static com.android.SdkConstants.TOOLS_URI; 28 import static com.android.SdkConstants.VALUE_FILL_PARENT; 29 import static com.android.SdkConstants.VALUE_MATCH_PARENT; 30 import static com.android.SdkConstants.VIEW_FRAGMENT; 31 import static com.android.SdkConstants.VIEW_INCLUDE; 32 import static com.android.ide.eclipse.adt.internal.editors.layout.gle2.LayoutMetadata.KEY_FRAGMENT_LAYOUT; 33 34 import com.android.SdkConstants; 35 import com.android.ide.common.rendering.api.ILayoutPullParser; 36 import com.android.ide.common.rendering.api.IProjectCallback; 37 import com.android.ide.common.res2.ValueXmlHelper; 38 import com.android.ide.eclipse.adt.internal.editors.layout.gle2.LayoutMetadata; 39 import com.google.common.collect.Maps; 40 41 import org.kxml2.io.KXmlParser; 42 43 import java.io.File; 44 import java.util.Map; 45 46 /** 47 * Modified {@link KXmlParser} that adds the methods of {@link ILayoutPullParser}, and 48 * performs other layout-specific parser behavior like translating fragment tags into 49 * include tags. 50 * <p/> 51 * It will return a given parser when queried for one through 52 * {@link ILayoutPullParser#getParser(String)} for a given name. 53 * 54 */ 55 public class ContextPullParser extends KXmlParser implements ILayoutPullParser { 56 private static final String COMMENT_PREFIX = "<!--"; //$NON-NLS-1$ 57 private static final String COMMENT_SUFFIX = "-->"; //$NON-NLS-1$ 58 /** The callback to request parsers from */ 59 private final IProjectCallback mProjectCallback; 60 /** The {@link File} for the layout currently being parsed */ 61 private File mFile; 62 /** The layout to be shown for the current {@code <fragment>} tag. Usually null. */ 63 private String mFragmentLayout = null; 64 65 /** 66 * Creates a new {@link ContextPullParser} 67 * 68 * @param projectCallback the associated callback 69 * @param file the file to be parsed 70 */ 71 public ContextPullParser(IProjectCallback projectCallback, File file) { 72 super(); 73 mProjectCallback = projectCallback; 74 mFile = file; 75 } 76 77 // --- Layout lib API methods 78 79 @Override 80 /** 81 * this is deprecated but must still be implemented for older layout libraries. 82 * @deprecated use {@link IProjectCallback#getParser(String)}. 83 */ 84 @Deprecated 85 public ILayoutPullParser getParser(String layoutName) { 86 return mProjectCallback.getParser(layoutName); 87 } 88 89 @Override 90 public Object getViewCookie() { 91 String name = super.getName(); 92 if (name == null) { 93 return null; 94 } 95 96 // Store tools attributes if this looks like a layout we'll need adapter view 97 // bindings for in the ProjectCallback. 98 if (LIST_VIEW.equals(name) 99 || EXPANDABLE_LIST_VIEW.equals(name) 100 || GRID_VIEW.equals(name) 101 || SPINNER.equals(name)) { 102 Map<String, String> map = null; 103 int count = getAttributeCount(); 104 for (int i = 0; i < count; i++) { 105 String namespace = getAttributeNamespace(i); 106 if (namespace != null && namespace.equals(TOOLS_URI)) { 107 String attribute = getAttributeName(i); 108 if (attribute.equals(ATTR_IGNORE)) { 109 continue; 110 } 111 if (map == null) { 112 map = Maps.newHashMapWithExpectedSize(4); 113 } 114 map.put(attribute, getAttributeValue(i)); 115 } 116 } 117 118 return map; 119 } 120 121 return null; 122 } 123 124 // --- KXMLParser override 125 126 @Override 127 public String getName() { 128 String name = super.getName(); 129 130 // At designtime, replace fragments with includes. 131 if (name.equals(VIEW_FRAGMENT)) { 132 mFragmentLayout = LayoutMetadata.getProperty(this, KEY_FRAGMENT_LAYOUT); 133 if (mFragmentLayout != null) { 134 return VIEW_INCLUDE; 135 } 136 } else { 137 mFragmentLayout = null; 138 } 139 140 141 return name; 142 } 143 144 @Override 145 public String getAttributeValue(String namespace, String localName) { 146 if (ATTR_LAYOUT.equals(localName) && mFragmentLayout != null) { 147 return mFragmentLayout; 148 } 149 150 String value = super.getAttributeValue(namespace, localName); 151 152 // on the fly convert match_parent to fill_parent for compatibility with older 153 // platforms. 154 if (VALUE_MATCH_PARENT.equals(value) && 155 (ATTR_LAYOUT_WIDTH.equals(localName) || 156 ATTR_LAYOUT_HEIGHT.equals(localName)) && 157 SdkConstants.NS_RESOURCES.equals(namespace)) { 158 return VALUE_FILL_PARENT; 159 } 160 161 // Handle unicode escapes etc 162 value = ValueXmlHelper.unescapeResourceString(value, false, false); 163 164 return value; 165 } 166 } 167