Exemplo n.º 1
0
  // Couldn't get MessageFormat to work, so I cheated
  public static OID valueOf(String s) throws IllegalArgumentException {
    StringTokenizer st = new StringTokenizer(s, "[:{}],");
    if (st.countTokens() < 2) {
      st = new StringTokenizer(java.net.URLDecoder.decode(s), "[:{}],");

      if (st.countTokens() < 2) {
        throw new IllegalArgumentException(
            "Invalid OID '" + s + "'. It must have at least the object " + "type and the value");
      }
    }

    String type = st.nextToken();

    try {
      // we know that the first item is the type of the object
      // represented by this OID
      OID oid = new OID(type);

      // for the rest of them, we are going to "split" on the
      // "=" sign
      int nTokens = 0;
      while (st.hasMoreTokens()) {
        String nextToken = st.nextToken();
        int index = nextToken.indexOf("=");
        String key = (nextToken.substring(0, index));
        String value = nextToken.substring(index + 1);

        // we need the key to loose the single space before it that
        // is created by the HashMap.  We cannot use trim because
        // we don't want to loose trailing spaces
        if (nTokens > 0) {
          key = key.substring(1);
        }

        // if it can be a BigDecimal, that is what we make it
        boolean bigDecimal = true;
        for (int i = 0; i < value.length(); i++) {
          char c = value.charAt(i);
          if (!('0' <= c && c <= '9') && !((i == 0) && (c == '-'))) {
            bigDecimal = false;
            break;
          }
        }

        if (bigDecimal) {
          oid.set(key, new BigDecimal(value));
        } else {
          oid.set(key, value);
        }
        nTokens++;
      }
      return oid;
    } catch (PersistenceException e) {
      throw new IllegalArgumentException(
          "Invalid OID '" + s + "'. The type specified [" + type + "] is not defined");
    }
  }
Exemplo n.º 2
0
  /**
   * Creates an OID with a single attribute for the key. To create a multi-valued OID, use a single
   * arg OID constructor, and add individual properties with the set method. This constructor should
   * be used when the object type being instantiated has a single primary key. For instance, if the
   * object type is <code>com.arsdigita.kernel.ACSObject</code> then the value should be the <code>
   * object_id</code>. So, if developers wanted to create the OID for ID zero, they would call
   * <code>new OID(acsObjectType, new BigDecimal(0))</code>. A <code>BigDecimal</code> is passed in
   * because the "id" attribute for the ACSObject type is declared as <code>BigDecimal</code> in the
   * PDL file.
   *
   * @param type The ObjectType of the ID
   * @param value The value of the ID
   * @exception PersistenceException will be thrown if the given object type does not have exactly a
   *     single key (if <code>type.getObjectMap().getObjectKey().getCount()
   *            != 1</code>).
   * @pre type != null
   * @pre type.getObjectMap().getObjectKey().getCount() == 1
   */
  public OID(ObjectType type, Object value) {
    this(type);
    Iterator it = type.getKeyProperties();
    if (!it.hasNext()) {
      throw new PersistenceException("Empty object key: " + type);
    }

    Property prop = (Property) it.next();

    if (it.hasNext()) {
      throw new PersistenceException("This object type has a compound key.");
    }

    String attr = prop.getName();
    set(attr, value);
  }