Ejemplo n.º 1
0
 /** Test key from kind and name. */
 public void testKeyFromKindAndName() {
   Key key = new Key(kind, name);
   assertEquals(kind, key.getKind());
   assertEquals(name, key.getName());
   assertNull(key.getParent());
   assertEquals(0L, key.getId());
 }
Ejemplo n.º 2
0
  @Test
  public void testMarshall_Embedded() {
    RootEntity rootObject = new RootEntity(); // one entity
    ChildEntity childObject = new ChildEntity("Test City");
    ChildEntity embeddedObject = new ChildEntity("Old Test City");
    embeddedObject.setId(1L);
    embeddedObject.setType("EmbeddedType");
    rootObject.setId("TestUser");
    rootObject.setCount(25);
    childObject.setParent(rootObject);
    rootObject.setNewChildEntity(childObject); // one entity
    rootObject.setEmbeddedEntity(embeddedObject); // not included, @Embedded

    IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, rootObject);

    Entity rootObjectEntity = stack.get(rootObject);
    Entity childObjectEntity = stack.get(childObject);

    EmbeddedEntity ee = (EmbeddedEntity) rootObjectEntity.getProperty("embeddedEntity");
    Key childKey = (Key) rootObjectEntity.getProperty("newChildEntity");
    Key parentKey = childKey.getParent();

    assertEquals(2, stack.size());
    assertNotNull(parentKey);
    assertEquals(EmbeddedEntity.class, ee.getClass());
    assertTrue(childKey instanceof Key);
    assertEquals(rootObjectEntity.getKey().getId(), parentKey.getId());
    assertEquals(rootObjectEntity.getKey().getName(), parentKey.getName());

    assertEquals(1L, ee.getProperties().get("id"));
    assertEquals("EmbeddedType", ee.getProperties().get("type"));
  }
Ejemplo n.º 3
0
 /** Test key from kind, parent and name. */
 public void testKeyFromKindParentAndName() {
   Key parent = new Key(kind);
   Key key = new Key(kind, parent, name);
   assertEquals(kind, key.getKind());
   assertEquals(name, key.getName());
   assertEquals(parent, key.getParent());
   assertEquals(0L, key.getId());
 }
Ejemplo n.º 4
0
 /** Test key from kind, parent and id. */
 public void testKeyFromKindParentAndId() {
   Key parent = new Key(kind);
   Key key = new Key(kind, parent, id);
   assertEquals(kind, key.getKind());
   assertNull(key.getName());
   assertEquals(id, key.getId());
   assertEquals(parent, key.getParent());
 }
Ejemplo n.º 5
0
 @Override
 public boolean equals(Object obj) {
   Key key = (Key) obj;
   if (this.name.equals(key.getName())) {
     return (this.bodyType == key.getBodyType());
   }
   return false;
 }
Ejemplo n.º 6
0
  /**
   * Compile a buildKeys() method into the output class. Note that keys for the input document are
   * created in topLevel(), not in this method. However, we still need this method to create keys
   * for documents loaded via the XPath document() function.
   */
  private String compileBuildKeys(ClassGenerator classGen) {
    final ConstantPoolGen cpg = classGen.getConstantPool();

    final com.sun.org.apache.bcel.internal.generic.Type[] argTypes = {
      Util.getJCRefType(DOM_INTF_SIG),
      Util.getJCRefType(NODE_ITERATOR_SIG),
      Util.getJCRefType(TRANSLET_OUTPUT_SIG),
      com.sun.org.apache.bcel.internal.generic.Type.INT
    };

    final String[] argNames = {DOCUMENT_PNAME, ITERATOR_PNAME, TRANSLET_OUTPUT_PNAME, "current"};

    final InstructionList il = new InstructionList();

    final MethodGenerator buildKeys =
        new MethodGenerator(
            ACC_PUBLIC,
            com.sun.org.apache.bcel.internal.generic.Type.VOID,
            argTypes,
            argNames,
            "buildKeys",
            _className,
            il,
            classGen.getConstantPool());

    buildKeys.addException("com.sun.org.apache.xalan.internal.xsltc.TransletException");

    final Enumeration elements = elements();
    while (elements.hasMoreElements()) {
      // xsl:key
      final Object element = elements.nextElement();
      if (element instanceof Key) {
        final Key key = (Key) element;
        key.translate(classGen, buildKeys);
        _keys.put(key.getName(), key);
      }
    }

    il.append(RETURN);

    // Compute max locals + stack and add method to class
    buildKeys.stripAttributes(true);
    buildKeys.setMaxLocals();
    buildKeys.setMaxStack();
    buildKeys.removeNOPs();

    classGen.addMethod(buildKeys.getMethod());

    return ("(" + DOM_INTF_SIG + NODE_ITERATOR_SIG + TRANSLET_OUTPUT_SIG + "I)V");
  }
Ejemplo n.º 7
0
  @Test
  public void testMarshall_Child() {
    RootEntity rootObject = new RootEntity(); // one entity
    ChildEntity childObject = new ChildEntity("Test City");
    rootObject.setId("TestUser");
    rootObject.setCount(25);
    childObject.setParent(rootObject);
    rootObject.setNewChildEntity(childObject); // one entity

    IdentityHashMap<Object, Entity> stack = testMarshaller.marshall(null, rootObject);

    Entity rootObjectEntity = stack.get(rootObject);
    Entity childObjectEntity = stack.get(childObject);

    Key childKey = (Key) rootObjectEntity.getProperty("newChildEntity");
    Key parentKey = childKey.getParent();

    assertEquals(2, stack.size());
    assertNotNull(parentKey);
    assertTrue(childKey instanceof Key);
    assertEquals(rootObjectEntity.getKey().getId(), parentKey.getId());
    assertEquals(rootObjectEntity.getKey().getName(), parentKey.getName());
  }
Ejemplo n.º 8
0
  /**
   * Compile a topLevel() method into the output class. This method is called from transform() to
   * handle all non-template top-level elements. Returns the signature of the topLevel() method.
   *
   * <p>Global variables/params and keys are first sorted to resolve dependencies between them. The
   * XSLT 1.0 spec does not allow a key to depend on a variable. However, for compatibility with
   * Xalan interpretive, that type of dependency is allowed. Note also that the buildKeys() method
   * is still generated as it is used by the LoadDocument class, but it no longer called from
   * transform().
   */
  private String compileTopLevel(ClassGenerator classGen) {

    final ConstantPoolGen cpg = classGen.getConstantPool();

    final com.sun.org.apache.bcel.internal.generic.Type[] argTypes = {
      Util.getJCRefType(DOM_INTF_SIG),
      Util.getJCRefType(NODE_ITERATOR_SIG),
      Util.getJCRefType(TRANSLET_OUTPUT_SIG)
    };

    final String[] argNames = {DOCUMENT_PNAME, ITERATOR_PNAME, TRANSLET_OUTPUT_PNAME};

    final InstructionList il = new InstructionList();

    final MethodGenerator toplevel =
        new MethodGenerator(
            ACC_PUBLIC,
            com.sun.org.apache.bcel.internal.generic.Type.VOID,
            argTypes,
            argNames,
            "topLevel",
            _className,
            il,
            classGen.getConstantPool());

    toplevel.addException("com.sun.org.apache.xalan.internal.xsltc.TransletException");

    // Define and initialize 'current' variable with the root node
    final LocalVariableGen current =
        toplevel.addLocalVariable(
            "current", com.sun.org.apache.bcel.internal.generic.Type.INT, null, null);

    final int setFilter =
        cpg.addInterfaceMethodref(
            DOM_INTF, "setFilter", "(Lcom/sun/org/apache/xalan/internal/xsltc/StripFilter;)V");

    final int gitr = cpg.addInterfaceMethodref(DOM_INTF, "getIterator", "()" + NODE_ITERATOR_SIG);
    il.append(toplevel.loadDOM());
    il.append(new INVOKEINTERFACE(gitr, 1));
    il.append(toplevel.nextNode());
    current.setStart(il.append(new ISTORE(current.getIndex())));

    // Create a new list containing variables/params + keys
    Vector varDepElements = new Vector(_globals);
    Enumeration elements = elements();
    while (elements.hasMoreElements()) {
      final Object element = elements.nextElement();
      if (element instanceof Key) {
        varDepElements.add(element);
      }
    }

    // Determine a partial order for the variables/params and keys
    varDepElements = resolveDependencies(varDepElements);

    // Translate vars/params and keys in the right order
    final int count = varDepElements.size();
    for (int i = 0; i < count; i++) {
      final TopLevelElement tle = (TopLevelElement) varDepElements.elementAt(i);
      tle.translate(classGen, toplevel);
      if (tle instanceof Key) {
        final Key key = (Key) tle;
        _keys.put(key.getName(), key);
      }
    }

    // Compile code for other top-level elements
    Vector whitespaceRules = new Vector();
    elements = elements();
    while (elements.hasMoreElements()) {
      final Object element = elements.nextElement();
      // xsl:decimal-format
      if (element instanceof DecimalFormatting) {
        ((DecimalFormatting) element).translate(classGen, toplevel);
      }
      // xsl:strip/preserve-space
      else if (element instanceof Whitespace) {
        whitespaceRules.addAll(((Whitespace) element).getRules());
      }
    }

    // Translate all whitespace strip/preserve rules
    if (whitespaceRules.size() > 0) {
      Whitespace.translateRules(whitespaceRules, classGen);
    }

    if (classGen.containsMethod(STRIP_SPACE, STRIP_SPACE_PARAMS) != null) {
      il.append(toplevel.loadDOM());
      il.append(classGen.loadTranslet());
      il.append(new INVOKEINTERFACE(setFilter, 2));
    }

    il.append(RETURN);

    // Compute max locals + stack and add method to class
    classGen.addMethod(toplevel);

    return ("(" + DOM_INTF_SIG + NODE_ITERATOR_SIG + TRANSLET_OUTPUT_SIG + ")V");
  }