XFADocument.java

  1. /*
  2.  * GovWay - A customizable API Gateway
  3.  * https://govway.org
  4.  *
  5.  * Copyright (c) 2005-2025 Link.it srl (https://link.it).
  6.  *
  7.  * This program is free software: you can redistribute it and/or modify
  8.  * it under the terms of the GNU General Public License version 3, as published by
  9.  * the Free Software Foundation.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18.  *
  19.  */
  20. package org.openspcoop2.utils.pdf;

  21. import java.io.ByteArrayOutputStream;

  22. import org.apache.pdfbox.pdmodel.interactive.form.PDXFAResource;
  23. import org.openspcoop2.utils.UtilsException;
  24. import org.openspcoop2.utils.xml.XMLUtils;
  25. import org.w3c.dom.Document;
  26. import org.w3c.dom.Element;
  27. import org.w3c.dom.Node;

  28. /**
  29. * XFADocument
  30. *
  31. * @author Andrea Poli (apoli@link.it)
  32. * @author $Author$
  33. * @version $Rev$, $Date$
  34. */
  35. public class XFADocument {

  36.     public static final String XFA_DATA_NAMESPACE = "http://www.xfa.org/schema/xfa-data/1.0/";
  37.     public static final String XFA_DATASETS_LOCAL_NAME = "datasets";
  38.     public static final String XFA_DATA_LOCAL_NAME = "data";
  39.    
  40.     public static byte[] addXfaDatasets(byte[] content) throws UtilsException {
  41.         try {
  42.             String xmlPrefix = "<xfa:"+XFA_DATASETS_LOCAL_NAME+" xmlns:xfa=\""+XFA_DATA_NAMESPACE+"\">\n"+
  43.                     "<xfa:"+XFA_DATA_LOCAL_NAME+">\n";
  44.             String xmlSuffix = "</xfa:"+XFA_DATA_LOCAL_NAME+">\n</xfa:"+XFA_DATASETS_LOCAL_NAME+">";
  45.                
  46.             ByteArrayOutputStream bout = null;
  47.             bout = new ByteArrayOutputStream();
  48.             bout.write(xmlPrefix.getBytes());
  49.             bout.write(content);
  50.             bout.write(xmlSuffix.getBytes());
  51.             bout.flush();
  52.             bout.close();  
  53.             return bout.toByteArray();
  54.         }catch(Exception e) {
  55.             throw new UtilsException(e.getMessage(),e);
  56.         }
  57.     }
  58.    
  59.     private PDXFAResource xfa;
  60.     private Document document;
  61.     private byte[] content;
  62.    
  63.     public PDXFAResource getXfa() {
  64.         return this.xfa;
  65.     }
  66.     public void setXfa(PDXFAResource xfa) {
  67.         this.xfa = xfa;
  68.     }
  69.     public Document getDocument() {
  70.         return this.document;
  71.     }
  72.     public void setDocument(Document document) {
  73.         this.document = document;
  74.     }
  75.     public byte[] getContent() {
  76.         return this.content;
  77.     }
  78.     public void setContent(byte[] content) {
  79.         this.content = content;
  80.     }
  81.    
  82.     public boolean isXfaDataContent() {
  83.         Node n = this.getXfaDataContentNode();
  84.         return n!=null;
  85.     }
  86.     public Node getXfaDataContentNode() {
  87.         if(this.document!=null && this.document.getDocumentElement()!=null) {
  88.             Element rootElement = this.document.getDocumentElement();
  89.             if(XFA_DATA_NAMESPACE.equals(rootElement.getNamespaceURI()) && XFA_DATASETS_LOCAL_NAME.equals(rootElement.getLocalName())) {
  90.                 XMLUtils xmlUtils = XMLUtils.getInstance();
  91.                 Node nData = xmlUtils.getFirstNotEmptyChildNode(rootElement, false);
  92.                 if(XFA_DATA_NAMESPACE.equals(nData.getNamespaceURI()) && XFA_DATA_LOCAL_NAME.equals(nData.getLocalName())) {
  93.                     return xmlUtils.getFirstNotEmptyChildNode(nData, false);
  94.                 }
  95.             }
  96.         }
  97.         return null;
  98.     }
  99.     public byte[] getXfaDataContent() throws UtilsException {
  100.         try {
  101.             byte[] contentData = null;
  102.             Node n = this.getXfaDataContentNode();
  103.             if(n!=null) {
  104.                 XMLUtils xmlUtils = XMLUtils.getInstance();
  105.                 contentData = xmlUtils.toByteArray(n, true);
  106.             }
  107.             return contentData;
  108.         }catch(Exception e) {
  109.             throw new UtilsException(e.getMessage(),e);
  110.         }
  111.     }

  112. }