Home | History | Annotate | Download | only in sdk
      1 /*
      2  * Copyright (C) 2009 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.sdk;
     18 
     19 
     20 import org.xml.sax.ErrorHandler;
     21 import org.xml.sax.SAXException;
     22 
     23 import java.io.InputStream;
     24 
     25 import javax.xml.XMLConstants;
     26 import javax.xml.transform.stream.StreamSource;
     27 import javax.xml.validation.Schema;
     28 import javax.xml.validation.SchemaFactory;
     29 import javax.xml.validation.Validator;
     30 
     31 /**
     32  * Public constants for the layout device description XML Schema.
     33  */
     34 public class LayoutDevicesXsd {
     35 
     36     /** The XML namespace of the layout-configs XML. */
     37     public static final String NS_LAYOUT_DEVICE_XSD =
     38         "http://schemas.android.com/sdk/android/layout-devices/1";                  //$NON-NLS-1$
     39 
     40     /**
     41      * The "layout-devices" element is the root element of this schema.
     42      *
     43      * It must contain one or more "device" elements that each define the configurations
     44      * available for a given device.
     45      *
     46      * These definitions are used in the Graphical Layout Editor in the
     47      * Android Development Tools (ADT) plugin for Eclipse.
     48      */
     49     public static final String NODE_LAYOUT_DEVICES = "layout-devices";              //$NON-NLS-1$
     50 
     51     /**
     52      * A device element must contain at most one "default" element followed
     53      * by one or more ""config" elements.
     54      *
     55      * The "default" element defines all the default parameters inherited
     56      * by the following "config" elements. Each "config" element can override
     57      * the default values, if any.
     58      *
     59      * A "device" element also has a required "name" attribute that represents
     60      * the user-interface name of this device.
     61      */
     62     public static final String NODE_DEVICE = "device";                              //$NON-NLS-1$
     63 
     64     /**
     65      * The "default" element contains zero or more of all the parameter elements
     66      * listed below. It defines all the parameters that are common to all
     67      * declared "config" elements.
     68      */
     69     public static final String NODE_DEFAULT = "default";                            //$NON-NLS-1$
     70 
     71     /**
     72      * The "config" element contains zero or more of all the parameter elements
     73      * listed below. The parameters from the "default" element (if present) are
     74      * automatically inherited and can be overridden.
     75      */
     76     public static final String NODE_CONFIG = "config";                              //$NON-NLS-1$
     77 
     78 
     79     public static final String NODE_COUNTRY_CODE = "country-code";                  //$NON-NLS-1$
     80 
     81     public static final String NODE_NETWORK_CODE = "network-code";                  //$NON-NLS-1$
     82 
     83     public static final String NODE_SCREEN_SIZE = "screen-size";                    //$NON-NLS-1$
     84 
     85     public static final String NODE_SCREEN_RATIO = "screen-ratio";                  //$NON-NLS-1$
     86 
     87     public static final String NODE_SCREEN_ORIENTATION = "screen-orientation";      //$NON-NLS-1$
     88 
     89     public static final String NODE_PIXEL_DENSITY = "pixel-density";                //$NON-NLS-1$
     90 
     91     public static final String NODE_TOUCH_TYPE = "touch-type";                      //$NON-NLS-1$
     92 
     93     public static final String NODE_KEYBOARD_STATE = "keyboard-state";              //$NON-NLS-1$
     94 
     95     public static final String NODE_TEXT_INPUT_METHOD = "text-input-method";        //$NON-NLS-1$
     96 
     97     public static final String NODE_NAV_STATE = "nav-state";                        //$NON-NLS-1$
     98 
     99     public static final String NODE_NAV_METHOD = "nav-method";                      //$NON-NLS-1$
    100 
    101     public static final String NODE_SCREEN_DIMENSION = "screen-dimension";          //$NON-NLS-1$
    102 
    103     /** The screen-dimension element has 2 size element children. */
    104     public static final String NODE_SIZE = "size";                                  //$NON-NLS-1$
    105 
    106     public static final String NODE_XDPI = "xdpi";                                  //$NON-NLS-1$
    107 
    108     public static final String NODE_YDPI = "ydpi";                                  //$NON-NLS-1$
    109 
    110     /**
    111      * The "name" attribute, used by both the "device" and the "config"
    112      * elements. It represents the user-interface name of these objects.
    113      */
    114     public static final String ATTR_NAME = "name";                                  //$NON-NLS-1$
    115 
    116     /**
    117      * Helper to get an input stream of the layout config XML schema.
    118      */
    119     public static InputStream getXsdStream() {
    120         return LayoutDevicesXsd.class.getResourceAsStream("layout-devices.xsd");    //$NON-NLS-1$
    121     }
    122 
    123     /** Helper method that returns a {@link Validator} for our XSD */
    124     public static Validator getValidator(ErrorHandler handler) throws SAXException {
    125         InputStream xsdStream = getXsdStream();
    126         SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    127         Schema schema = factory.newSchema(new StreamSource(xsdStream));
    128         Validator validator = schema.newValidator();
    129         if (handler != null) {
    130             validator.setErrorHandler(handler);
    131         }
    132 
    133         return validator;
    134     }
    135 
    136 }
    137