Пример #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());
      }
    }
  }
Пример #2
0
  @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
      }
    }
  }
Пример #3
0
  /**
   * Reads an XML element into a bean property by first locating the XML element corresponding to
   * this property.
   *
   * @param ob The bean whose property is being set
   * @param desc The property that will be set
   * @param nodes The list of XML items that may contain the property
   * @throws IOException If there is an error reading the document
   */
  public void readProperty(Object ob, PropertyDescriptor desc, NodeList nodes, NamedNodeMap attrs)
      throws IOException {
    int numAttrs = attrs.getLength();

    for (int i = 0; i < numAttrs; i++) {
      // See if the attribute name matches the property name
      if (namesMatch(desc.getName(), attrs.item(i).getNodeName())) {
        // Get the method used to set this property
        Method setter = desc.getWriteMethod();

        // If this object has no setter, don't bother writing it
        if (setter == null) continue;

        // Get the value of the property
        Object obValue = getObjectValue(desc, attrs.item(i).getNodeValue());
        if (obValue != null) {
          try {
            // Set the property value
            setter.invoke(ob, new Object[] {obValue});
          } catch (InvocationTargetException exc) {
            throw new IOException(
                "Error setting property " + desc.getName() + ": " + exc.toString());
          } catch (IllegalAccessException exc) {
            throw new IOException(
                "Error setting property " + desc.getName() + ": " + exc.toString());
          }
        }

        return;
      }
    }

    int numNodes = nodes.getLength();

    Vector arrayBuild = null;

    for (int i = 0; i < numNodes; i++) {
      Node node = nodes.item(i);

      // If this node isn't an element, skip it
      if (!(node instanceof Element)) continue;

      Element element = (Element) node;

      // See if the tag name matches the property name
      if (namesMatch(desc.getName(), element.getTagName())) {
        // Get the method used to set this property
        Method setter = desc.getWriteMethod();

        // If this object has no setter, don't bother writing it
        if (setter == null) continue;

        // Get the value of the property
        Object obValue = getObjectValue(desc, element);

        // 070201 MAW: Modified from change submitted by Steve Poulson
        if (setter.getParameterTypes()[0].isArray()) {
          if (arrayBuild == null) {
            arrayBuild = new Vector();
          }
          arrayBuild.addElement(obValue);

          // 070201 MAW: Go ahead and read through the rest of the nodes in case
          //             another one matches the array. This has the effect of skipping
          //             over the "return" statement down below
          continue;
        }

        if (obValue != null) {
          try {
            // Set the property value
            setter.invoke(ob, new Object[] {obValue});
          } catch (InvocationTargetException exc) {
            throw new IOException(
                "Error setting property " + desc.getName() + ": " + exc.toString());
          } catch (IllegalAccessException exc) {
            throw new IOException(
                "Error setting property " + desc.getName() + ": " + exc.toString());
          }
        }
        return;
      }
    }

    // If we build a vector of array members, convert the vector into
    // an array and save it in the property
    if (arrayBuild != null) {
      // Get the method used to set this property
      Method setter = desc.getWriteMethod();

      if (setter == null) return;

      Object[] obValues = (Object[]) Array.newInstance(desc.getPropertyType(), arrayBuild.size());

      arrayBuild.copyInto(obValues);

      try {
        setter.invoke(ob, new Object[] {obValues});
      } catch (InvocationTargetException exc) {
        throw new IOException("Error setting property " + desc.getName() + ": " + exc.toString());
      } catch (IllegalAccessException exc) {
        throw new IOException("Error setting property " + desc.getName() + ": " + exc.toString());
      }

      return;
    }
  }
Пример #4
0
  /**
   * Reads XML element(s) into an indexed bean property by first locating the XML element(s)
   * corresponding to this property.
   *
   * @param ob The bean whose property is being set
   * @param desc The property that will be set
   * @param nodes The list of XML items that may contain the property
   * @throws IOException If there is an error reading the document
   */
  public void readIndexedProperty(
      Object ob, IndexedPropertyDescriptor desc, NodeList nodes, NamedNodeMap attrs)
      throws IOException {
    // Create a vector to hold the property values
    Vector v = new Vector();

    int numAttrs = attrs.getLength();

    for (int i = 0; i < numAttrs; i++) {
      // See if this attribute matches the property name
      if (namesMatch(desc.getName(), attrs.item(i).getNodeName())) {
        // Get the property value
        Object obValue = getObjectValue(desc, attrs.item(i).getNodeValue());

        if (obValue != null) {
          // Add the value to the list of values to be set
          v.addElement(obValue);
        }
      }
    }

    int numNodes = nodes.getLength();

    for (int i = 0; i < numNodes; i++) {
      Node node = nodes.item(i);

      // Skip non-element nodes
      if (!(node instanceof Element)) continue;

      Element element = (Element) node;

      // See if this element tag matches the property name
      if (namesMatch(desc.getName(), element.getTagName())) {
        // Get the property value
        Object obValue = getObjectValue(desc, element);

        if (obValue != null) {
          // Add the value to the list of values to be set
          v.addElement(obValue);
        }
      }
    }

    // Get the method used to set the property value
    Method setter = desc.getWriteMethod();

    // If this property has no setter, don't write it
    if (setter == null) return;

    // Create a new array of property values
    Object propArray = Array.newInstance(desc.getPropertyType().getComponentType(), v.size());

    // Copy the vector into the array
    v.copyInto((Object[]) propArray);

    try {
      // Store the array of property values
      setter.invoke(ob, new Object[] {propArray});
    } catch (InvocationTargetException exc) {
      throw new IOException("Error setting property " + desc.getName() + ": " + exc.toString());
    } catch (IllegalAccessException exc) {
      throw new IOException("Error setting property " + desc.getName() + ": " + exc.toString());
    }
  }
  /*
   * 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);
    }
  }