Home | History | Annotate | Download | only in layout
      1 /*
      2  * Copyright (C) 2008 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 com.android.SdkConstants;
     20 import com.android.ide.common.rendering.api.ILayoutPullParser;
     21 import com.android.ide.eclipse.adt.AdtConstants;
     22 import com.android.ide.eclipse.adt.internal.editors.layout.descriptors.ViewElementDescriptor;
     23 import com.android.layoutlib.api.ILayoutResult.ILayoutViewInfo;
     24 
     25 import org.xmlpull.v1.XmlPullParserException;
     26 
     27 /**
     28  * {@link ILayoutPullParser} implementation to render android widget bitmap.
     29  * <p/>
     30  * The parser emulates a layout that contains just one widget, described by the
     31  * {@link ViewElementDescriptor} passed in the constructor.
     32  * <p/>
     33  * This pull parser generates {@link ILayoutViewInfo}s which key is a {@link ViewElementDescriptor}.
     34  */
     35 public class WidgetPullParser extends BasePullParser {
     36 
     37     private final ViewElementDescriptor mDescriptor;
     38     private String[][] mAttributes = new String[][] {
     39             { "text", null },
     40             { "layout_width", "wrap_content" },
     41             { "layout_height", "wrap_content" },
     42     };
     43 
     44     public WidgetPullParser(ViewElementDescriptor descriptor) {
     45         mDescriptor = descriptor;
     46 
     47         String[] segments = mDescriptor.getFullClassName().split(AdtConstants.RE_DOT);
     48         mAttributes[0][1] = segments[segments.length-1];
     49     }
     50 
     51     @Override
     52     public Object getViewCookie() {
     53         // we need a viewKey or the ILayoutResult will not contain any ILayoutViewInfo
     54         return mDescriptor;
     55     }
     56 
     57     /**
     58      * Legacy method required by {@link com.android.layoutlib.api.IXmlPullParser}
     59      */
     60     @Override
     61     public Object getViewKey() {
     62         return getViewCookie();
     63     }
     64 
     65     @Override
     66     public ILayoutPullParser getParser(String layoutName) {
     67         // there's no embedded layout for a single widget.
     68         return null;
     69     }
     70 
     71     @Override
     72     public int getAttributeCount() {
     73         return mAttributes.length; // text attribute
     74     }
     75 
     76     @Override
     77     public String getAttributeName(int index) {
     78         if (index < mAttributes.length) {
     79             return mAttributes[index][0];
     80         }
     81 
     82         return null;
     83     }
     84 
     85     @Override
     86     public String getAttributeNamespace(int index) {
     87         return SdkConstants.NS_RESOURCES;
     88     }
     89 
     90     @Override
     91     public String getAttributePrefix(int index) {
     92         // pass
     93         return null;
     94     }
     95 
     96     @Override
     97     public String getAttributeValue(int index) {
     98         if (index < mAttributes.length) {
     99             return mAttributes[index][1];
    100         }
    101 
    102         return null;
    103     }
    104 
    105     @Override
    106     public String getAttributeValue(String ns, String name) {
    107         if (SdkConstants.NS_RESOURCES.equals(ns)) {
    108             for (String[] attribute : mAttributes) {
    109                 if (name.equals(attribute[0])) {
    110                     return attribute[1];
    111                 }
    112             }
    113         }
    114 
    115         return null;
    116     }
    117 
    118     @Override
    119     public int getDepth() {
    120         // pass
    121         return 0;
    122     }
    123 
    124     @Override
    125     public String getName() {
    126         return mDescriptor.getXmlLocalName();
    127     }
    128 
    129     @Override
    130     public String getNamespace() {
    131         // pass
    132         return null;
    133     }
    134 
    135     @Override
    136     public String getPositionDescription() {
    137         // pass
    138         return null;
    139     }
    140 
    141     @Override
    142     public String getPrefix() {
    143         // pass
    144         return null;
    145     }
    146 
    147     @Override
    148     public boolean isEmptyElementTag() throws XmlPullParserException {
    149         if (mParsingState == START_TAG) {
    150             return true;
    151         }
    152 
    153         throw new XmlPullParserException("Call to isEmptyElementTag while not in START_TAG",
    154                 this, null);
    155     }
    156 
    157     @Override
    158     public void onNextFromStartDocument() {
    159         // just go to start_tag
    160         mParsingState = START_TAG;
    161     }
    162 
    163     @Override
    164     public void onNextFromStartTag() {
    165         // since we have no children, just go to end_tag
    166         mParsingState = END_TAG;
    167     }
    168 
    169     @Override
    170     public void onNextFromEndTag() {
    171         // just one tag. we are done.
    172         mParsingState = END_DOCUMENT;
    173     }
    174 }
    175