Пример #1
0
 /** @see railo.runtime.type.Collection#_setEL(java.lang.String, java.lang.Object) */
 public Object setEL(Collection.Key key, Object value) {
   try {
     return setEL(Caster.toIntValue(key.getString()), value);
   } catch (ExpressionException e) {
     return null;
   }
 }
Пример #2
0
 /** @see railo.runtime.type.Collection#setEL(java.lang.String, java.lang.Object) */
 public Object setEL(String key, Object value) {
   try {
     return setEL(Caster.toIntValue(key), value);
   } catch (ExpressionException e) {
     return null;
   }
 }
Пример #3
0
 public Object get(Key key) throws PageException {
   int row = Caster.toIntValue(key, Integer.MIN_VALUE);
   if (row == Integer.MIN_VALUE) {
     Object child = getChildElement(key, null);
     if (child != null) return child;
     throw new DatabaseException("key [" + key + "] not found", null, null, null);
   }
   return get(row);
 }
Пример #4
0
 public Object get(Key key, Object defaultValue) {
   int row = Caster.toIntValue(key, Integer.MIN_VALUE);
   if (row == Integer.MIN_VALUE) {
     Object child = getChildElement(key, null);
     if (child != null) return child;
     return defaultValue;
   }
   return get(row, defaultValue);
 }
Пример #5
0
  private void doActionAddWatermark() throws PageException, IOException, DocumentException {
    required("pdf", "addWatermark", "source", source);
    if (copyFrom == null && image == null)
      throw new ApplicationException(
          "at least one of the following attributes must be defined " + "[copyFrom,image]");

    if (destination != null && destination.exists() && !overwrite)
      throw new ApplicationException("destination file [" + destination + "] already exists");

    // image
    Image img = null;
    if (image != null) {
      railo.runtime.img.Image ri =
          railo.runtime.img.Image.createImage(pageContext, image, false, false, true);
      img = Image.getInstance(ri.getBufferedImage(), null, false);
    }
    // copy From
    else {
      byte[] barr;
      try {
        Resource res = Caster.toResource(pageContext, copyFrom, true);
        barr = IOUtil.toBytes(res);
      } catch (ExpressionException ee) {
        barr = Caster.toBinary(copyFrom);
      }
      img = Image.getInstance(PDFUtil.toImage(barr, 1).getBufferedImage(), null, false);
    }

    // position
    float x = UNDEFINED, y = UNDEFINED;
    if (!StringUtil.isEmpty(position)) {
      int index = position.indexOf(',');
      if (index == -1)
        throw new ApplicationException(
            "attribute [position] has a invalid value ["
                + position
                + "],"
                + "value should follow one of the following pattern [40,50], [40,] or [,50]");
      String strX = position.substring(0, index).trim();
      String strY = position.substring(index + 1).trim();
      if (!StringUtil.isEmpty(strX)) x = Caster.toIntValue(strX);
      if (!StringUtil.isEmpty(strY)) y = Caster.toIntValue(strY);
    }

    PDFDocument doc = toPDFDocument(source, password, null);
    doc.setPages(pages);
    PdfReader reader = doc.getPdfReader();
    reader.consolidateNamedDestinations();
    boolean destIsSource =
        destination != null && doc.getResource() != null && destination.equals(doc.getResource());
    java.util.List bookmarks = SimpleBookmark.getBookmark(reader);
    ArrayList master = new ArrayList();
    if (bookmarks != null) master.addAll(bookmarks);

    // output
    OutputStream os = null;
    if (!StringUtil.isEmpty(name) || destIsSource) {
      os = new ByteArrayOutputStream();
    } else if (destination != null) {
      os = destination.getOutputStream();
    }

    try {

      int len = reader.getNumberOfPages();
      PdfStamper stamp = new PdfStamper(reader, os);

      if (len > 0) {
        if (x == UNDEFINED || y == UNDEFINED) {
          PdfImportedPage first = stamp.getImportedPage(reader, 1);
          if (y == UNDEFINED) y = (first.getHeight() - img.getHeight()) / 2;
          if (x == UNDEFINED) x = (first.getWidth() - img.getWidth()) / 2;
        }
        img.setAbsolutePosition(x, y);
        // img.setAlignment(Image.ALIGN_JUSTIFIED); ration geht nicht anhand mitte

      }

      // rotation
      if (rotation != 0) {
        img.setRotationDegrees(rotation);
      }

      Set _pages = doc.getPages();
      for (int i = 1; i <= len; i++) {
        if (_pages != null && !_pages.contains(Integer.valueOf(i))) continue;
        PdfContentByte cb = foreground ? stamp.getOverContent(i) : stamp.getUnderContent(i);
        PdfGState gs1 = new PdfGState();
        // print.out("op:"+opacity);
        gs1.setFillOpacity(opacity);
        // gs1.setStrokeOpacity(opacity);
        cb.setGState(gs1);
        cb.addImage(img);
      }
      if (bookmarks != null) stamp.setOutlines(master);
      stamp.close();
    } finally {
      IOUtil.closeEL(os);
      if (os instanceof ByteArrayOutputStream) {
        if (destination != null)
          IOUtil.copy(
              new ByteArrayInputStream(((ByteArrayOutputStream) os).toByteArray()),
              destination,
              true); // MUST overwrite
        if (!StringUtil.isEmpty(name)) {
          pageContext.setVariable(
              name, new PDFDocument(((ByteArrayOutputStream) os).toByteArray(), password));
        }
      }
    }
  }
Пример #6
0
  /**
   * returns a property from a XMl Node
   *
   * @param node
   * @param key
   * @param caseSensitive
   * @return Object matching key
   * @throws SAXException
   */
  public static Object getProperty(Node node, Collection.Key k, boolean caseSensitive)
      throws SAXException {
    // String lcKey=StringUtil.toLowerCase(key);
    if (k.getLowerString().startsWith("xml")) {
      // Comment
      if (k.equals(XMLCOMMENT)) {
        StringBuffer sb = new StringBuffer();
        NodeList list = node.getChildNodes();
        int len = list.getLength();
        for (int i = 0; i < len; i++) {
          Node n = list.item(i);
          if (n instanceof Comment) {
            sb.append(((Comment) n).getData());
          }
        }
        return sb.toString();
      }
      // NS URI
      if (k.equals(XMLNSURI)) {
        undefinedInRoot(k, node);
        return param(node.getNamespaceURI(), "");
      }
      // Prefix
      if (k.equals(XMLNSPREFIX)) {
        undefinedInRoot(k, node);
        return param(node.getPrefix(), "");
      }
      // Root
      else if (k.equals(XMLROOT)) {
        Element re = getRootElement(node, caseSensitive);
        if (re == null)
          throw new SAXException(
              "Attribute [" + k.getString() + "] not found in XML, XML is empty");
        return param(re, "");
      }
      // Parent
      else if (k.equals(XMLPARENT)) {

        Node parent = getParentNode(node, caseSensitive);
        if (parent == null) {
          if (node.getNodeType() == Node.DOCUMENT_NODE)
            throw new SAXException(
                "Attribute ["
                    + k.getString()
                    + "] not found in XML, there is no parent element, you are already at the root element");
          throw new SAXException(
              "Attribute [" + k.getString() + "] not found in XML, there is no parent element");
        }
        return parent;
      }
      // Name
      else if (k.equals(XMLNAME)) {
        return node.getNodeName();
      }
      // Value
      else if (k.equals(XMLVALUE)) {
        return StringUtil.toStringEmptyIfNull(node.getNodeValue());
      }
      // type
      else if (k.equals(XMLTYPE)) {
        return getTypeAsString(node, true);
      }
      // Attributes
      else if (k.equals(XMLATTRIBUTES)) {
        NamedNodeMap attr = node.getAttributes();

        if (attr == null) throw undefined(k, node);
        return new XMLAttributes(node.getOwnerDocument(), attr, caseSensitive);
      }
      // Text
      else if (k.equals(XMLTEXT)) {
        undefinedInRoot(k, node);
        StringBuffer sb = new StringBuffer();
        NodeList list = node.getChildNodes();
        int len = list.getLength();
        for (int i = 0; i < len; i++) {
          Node n = list.item(i);
          if (n instanceof Text || n instanceof CDATASection) {
            sb.append(((CharacterData) n).getData());
          }
        }
        return sb.toString();
      } else if (k.equals(XMLCDATA)) {
        undefinedInRoot(k, node);
        StringBuffer sb = new StringBuffer();
        NodeList list = node.getChildNodes();
        int len = list.getLength();
        for (int i = 0; i < len; i++) {
          Node n = list.item(i);
          if (n instanceof Text || n instanceof CDATASection) {
            sb.append(((CharacterData) n).getData());
          }
        }
        return sb.toString();
      }
      // children
      else if (k.equals(XMLCHILDREN)) {
        return new XMLNodeList(node, caseSensitive);
      }
    }

    if (node instanceof Document) {
      node = ((Document) node).getDocumentElement();
      if (node == null)
        throw new SAXException("Attribute [" + k.getString() + "] not found in XML, XML is empty");

      // if((!caseSensitive && node.getNodeName().equalsIgnoreCase(k.getString())) || (caseSensitive
      // && node.getNodeName().equals(k.getString()))) {
      if (nameEqual(node, k.getString(), caseSensitive)) {
        return XMLStructFactory.newInstance(node, caseSensitive);
      }
    } else if (node.getNodeType() == Node.ELEMENT_NODE && Decision.isInteger(k)) {
      int index = Caster.toIntValue(k, 0);
      int count = 0;
      Node parent = node.getParentNode();
      String nodeName = node.getNodeName();
      Element[] children = XMLUtil.getChildElementsAsArray(parent);

      for (int i = 0; i < children.length; i++) {
        if (XMLUtil.nameEqual(children[i], nodeName, caseSensitive)) count++;

        if (count == index) return XMLCaster.toXMLStruct(children[i], caseSensitive);
      }
      String detail;
      if (count == 0) detail = "there are no Elements with this name";
      else if (count == 1) detail = "there is only 1 Element with this name";
      else detail = "there are only " + count + " Elements with this name";
      throw new SAXException(
          "invalid index ["
              + k.getString()
              + "] for Element with name ["
              + node.getNodeName()
              + "], "
              + detail);
    } else {
      List<Node> children =
          XMLUtil.getChildNodesAsList(node, Node.ELEMENT_NODE, caseSensitive, null);
      int len = children.size();
      Array array = null; // new ArrayImpl();
      Element el;
      XMLStruct sct = null, first = null;
      for (int i = 0; i < len; i++) {
        el = (Element) children.get(i); // XMLCaster.toXMLStruct(getChildNode(index),caseSensitive);
        if (XMLUtil.nameEqual(el, k.getString(), caseSensitive)) {
          sct = XMLCaster.toXMLStruct(el, caseSensitive);

          if (array != null) {
            array.appendEL(sct);
          } else if (first != null) {
            array = new ArrayImpl();
            array.appendEL(first);
            array.appendEL(sct);
          } else {
            first = sct;
          }
        }
      }

      if (array != null) {
        try {
          return new XMLMultiElementStruct(array, false);
        } catch (PageException e) {
        }
      }
      if (first != null) return first;
    }
    throw new SAXException("Attribute [" + k.getString() + "] not found");
  }
Пример #7
0
 /** @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key, java.lang.Object) */
 public Object get(Collection.Key key, Object defaultValue) {
   double index = Caster.toIntValue(key.getString(), Integer.MIN_VALUE);
   if (index == Integer.MIN_VALUE) return defaultValue;
   return get((int) index, defaultValue);
 }
Пример #8
0
 /** @see railo.runtime.type.Collection#get(java.lang.String, java.lang.Object) */
 public Object get(String key, Object defaultValue) {
   double index = Caster.toIntValue(key, Integer.MIN_VALUE);
   if (index == Integer.MIN_VALUE) return defaultValue;
   return get((int) index, defaultValue);
 }
Пример #9
0
 /** @see railo.runtime.type.Collection#get(railo.runtime.type.Collection.Key) */
 public Object get(Collection.Key key) throws ExpressionException {
   return getE(Caster.toIntValue(key.getString()));
 }
Пример #10
0
 /** @see railo.runtime.type.Collection#get(java.lang.String) */
 public Object get(String key) throws ExpressionException {
   return getE(Caster.toIntValue(key));
 }
Пример #11
0
 public Object removeEL(Collection.Key key) {
   return removeEL(Caster.toIntValue(key.getString(), -1));
 }
Пример #12
0
 /** @see railo.runtime.type.Collection#_set(java.lang.String, java.lang.Object) */
 public Object set(Collection.Key key, Object value) throws ExpressionException {
   return setE(Caster.toIntValue(key.getString()), value);
 }
Пример #13
0
 /** @see railo.runtime.type.Collection#set(java.lang.String, java.lang.Object) */
 public Object set(String key, Object value) throws ExpressionException {
   return setE(Caster.toIntValue(key), value);
 }
Пример #14
0
 /**
  * execute a multiply operation
  *
  * @param sql
  * @param qr QueryResult to execute on it
  * @param expression
  * @param row row of resultset to execute
  * @return result
  * @throws PageException
  */
 private Object executeExponent(PageContext pc, SQL sql, Query qr, ZExpression expression, int row)
     throws PageException {
   return Integer.valueOf(
       Caster.toIntValue(executeExp(pc, sql, qr, expression.getOperand(0), row))
           ^ Caster.toIntValue(executeExp(pc, sql, qr, expression.getOperand(1), row)));
 }