Beispiel #1
0
  /**
   * Examines a property's type to see which method should be used to parse the property's value.
   *
   * @param desc The description of the property
   * @param element The XML element containing the property value
   * @return The value stored in the element
   * @throws IOException If there is an error reading the document
   */
  public Object getObjectValue(PropertyDescriptor desc, Element element) throws IOException {
    // Find out what kind of property it is
    Class type = desc.getPropertyType();

    // If it's an array, get the base type
    if (type.isArray()) {
      type = type.getComponentType();
    }

    // For native types, object wrappers for natives, and strings, use the
    // basic parse routine
    if (type.equals(Integer.TYPE)
        || type.equals(Long.TYPE)
        || type.equals(Short.TYPE)
        || type.equals(Byte.TYPE)
        || type.equals(Boolean.TYPE)
        || type.equals(Float.TYPE)
        || type.equals(Double.TYPE)
        || Integer.class.isAssignableFrom(type)
        || Long.class.isAssignableFrom(type)
        || Short.class.isAssignableFrom(type)
        || Byte.class.isAssignableFrom(type)
        || Boolean.class.isAssignableFrom(type)
        || Float.class.isAssignableFrom(type)
        || Double.class.isAssignableFrom(type)
        || String.class.isAssignableFrom(type)) {
      return readBasicType(type, element);
    } else if (java.util.Date.class.isAssignableFrom(type)) {
      // If it's a date, use the date parser
      return readDate(element);
    } else {
      try {
        // If it's an object, create a new instance of the object (it should
        // be a bean, or there will be trouble)
        Object newOb = type.newInstance();

        // Copy the XML element into the bean
        readObject(newOb, element);

        return newOb;
      } catch (InstantiationException exc) {
        throw new IOException(
            "Error creating object for " + desc.getName() + ": " + exc.toString());
      } catch (IllegalAccessException exc) {
        throw new IOException(
            "Error creating object for " + desc.getName() + ": " + exc.toString());
      }
    }
  }
  @Override
  public void readFields(DataInput in) throws IOException {
    // construct matrix
    values = new Writable[in.readInt()][];
    for (int i = 0; i < values.length; i++) {
      values[i] = new Writable[in.readInt()];
    }

    // construct values
    for (int i = 0; i < values.length; i++) {
      for (int j = 0; j < values[i].length; j++) {
        Writable value; // construct value
        try {
          value = (Writable) valueClass.newInstance();
        } catch (InstantiationException e) {
          throw new RuntimeException(e.toString());
        } catch (IllegalAccessException e) {
          throw new RuntimeException(e.toString());
        }
        value.readFields(in); // read a value
        values[i][j] = value; // store it in values
      }
    }
  }
  /*
   * Create and open a Connection.
   * <p>
   * There are 2 ways to find a connection implementation.
   * 1. Get the class name from a system property generated in the form of:
   * <pre>
   *    j2me.{connection protocol}.protocol
   * </pre>
   * 2. Use the class root (see getClassRoot) and the connection
   * protocol to dynamically construct a class name in the the form of:
   * <pre>
   *   {class root}.j2me.{connection protocol}.Protocol
   * </pre>
   * The connection protocol is parsed from the <code>name</code> parameter
   * which takes the form of:
   * <pre>
   *   {connection protocol}:{protocol specific part}
   * </pre>
   * In order to avoid problems with illegal
   * class file names, all the '-' characters in the connection protocol
   * are automatically converted into '_' characters.
   * <p>
   * Additionally the protocol specific part is parsed from the name
   * parameter and passed to the connection's factory method.
   *
   * @param name             The URL for the connection
   * @param mode             The access mode
   * @param timeouts         A flag to indicate that the caller
   *                         wants timeout exceptions
   *
   * @return                 A new Connection object
   *
   * @exception IllegalArgumentException If a parameter is invalid.
   * @exception ConnectionNotFoundException If the target of the
   *   name cannot be found, or if the requested protocol type
   *   is not supported.
   * @exception IOException If some other kind of I/O error occurs.
   * @exception IllegalArgumentException If a parameter is invalid.
   */
  public Connection open(String name, int mode, boolean timeouts) throws IOException {
    String oemPrefix = "";
    /* Test for null argument */
    if (name == null) {
      throw new IllegalArgumentException("Null URL");
    }
    try {
      /*
       * Check for OEM specific http and https handler override.
       */
      oemPrefix =
          (String)
              java.security.AccessController.doPrivileged(
                  new GetPropertyAction("oem.http.handler.prefix", ""));
    } catch (Throwable t) {
      // do nothing
    }

    if (name.startsWith("http")) {
      name = oemPrefix + name;
    }

    /* Look for : as in "http:", "file:", or whatever */
    int colon = name.indexOf(':');
    if (colon == -1) {
      throw new IllegalArgumentException("Illegal protocol");
    }
    try {
      String protocol;

      /* Strip off the protocol name */
      protocol = name.substring(0, colon).toLowerCase();
      checkProtocol(protocol);
      /* Strip off the rest of the string */
      name = name.substring(colon + 1);

      /*
       * Convert all the '-' characters in the protocol
       * name to '_' characters (dashes are not allowed
       * in class names).  This operation creates garbage
       * only if the protocol name actually contains dashes
       */
      protocol = protocol.replace('-', '_');

      /*
       * Use the platform and protocol names to look up
       * a class to implement the connection
       */
      String className = getClassRoot() + "." + "j2me" + "." + protocol + ".Protocol";
      Class clazz = Class.forName(className, true, getProtocolClassLoader());

      /* Construct a new instance of the protocol */
      ConnectionBaseInterface uc = (ConnectionBaseInterface) clazz.newInstance();

      /* Open the connection, and return it */
      return uc.openPrim(name, mode, timeouts);
    } catch (InstantiationException x) {
      throw new IOException(x.toString());
    } catch (IllegalAccessException x) {
      throw new IOException(x.toString());
    } catch (ClassCastException x) {
      throw new IOException(x.toString());
    } catch (ClassNotFoundException x) {
      throw new ConnectionNotFoundException("The requested protocol does not exist " + name);
    }
  }