LSInputImpl.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.xml;

  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.InputStream;
  24. import java.io.InputStreamReader;
  25. import java.io.Reader;
  26. import java.nio.charset.Charset;

  27. import org.openspcoop2.utils.CopyCharStream;
  28. import org.openspcoop2.utils.CopyStream;
  29. import org.w3c.dom.ls.LSInput;

  30. /**
  31.  * Classe utilizzabile per raccogliere uno schema XSD
  32.  *
  33.  * @author Andrea Poli (apoli@link.it)
  34.  * @author $Author$
  35.  * @version $Rev$, $Date$
  36.  */

  37. public class LSInputImpl implements LSInput {

  38.     private String type;  // http://www.w3.org/2001/XMLSchema
  39.     private String namespaceURI;
  40.     private String publicId;
  41.     private String systemId; // es. esempio.xsd
  42.     private String baseURI; // es. /etc/govway/esempio.xsd
  43.     private byte[] resource;
  44.     private String encoding = Charset.defaultCharset().name();

  45.     public LSInputImpl(String type, String namespaceURI, String publicId, String systemId,
  46.             String baseURI, byte[] resource){
  47.         this.type = type;
  48.         this.namespaceURI = namespaceURI;
  49.         this.publicId = publicId;
  50.         this.systemId = systemId;
  51.         this.baseURI = baseURI;
  52.         this.resource = resource;
  53.     }
  54.    
  55.     @Override
  56.     public Reader getCharacterStream() {
  57.         try{
  58.             return new InputStreamReader(new ByteArrayInputStream(this.resource),this.encoding);
  59.         }catch(Exception e){
  60.             throw new RuntimeException("Metodo getCharacterStream() ha causato un errore",e);
  61.         }
  62.     }

  63.     @Override
  64.     public void setCharacterStream(Reader characterStream) {
  65.         try{
  66.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  67. //          int letti = 0;
  68. //          char [] buffer = new char[Utilities.DIMENSIONE_BUFFER];
  69. //          while( (letti=characterStream.read(buffer)) != -1 ){
  70. //              for(int i=0;i<letti;i++)
  71. //                  bout.write(buffer[i]);
  72. //          }
  73.             CopyCharStream.copy(characterStream, bout);
  74.             this.resource = bout.toByteArray();
  75.         }catch(Exception e){
  76.             throw new RuntimeException("Metodo setCharacterStream(Reader characterStream) ha causato un errore",e);
  77.         }
  78.     }

  79.     @Override
  80.     public InputStream getByteStream() {
  81.         return new ByteArrayInputStream(this.resource);
  82.     }

  83.     @Override
  84.     public void setByteStream(InputStream byteStream) {
  85.         try{
  86.             ByteArrayOutputStream bout = new ByteArrayOutputStream();
  87. //          int letti = 0;
  88. //          byte[] buffer = new byte[Utilities.DIMENSIONE_BUFFER];
  89. //          while( (letti=byteStream.read(buffer)) != -1 ){
  90. //              for(int i=0;i<letti;i++)
  91. //                  bout.write(buffer,0,letti);
  92. //          }
  93.             CopyStream.copy(byteStream, bout);
  94.             this.resource = bout.toByteArray();
  95.         }catch(Exception e){
  96.             throw new RuntimeException("Metodo setByteStream(InputStream byteStream) ha causato un errore",e);
  97.         }
  98.     }

  99.     @Override
  100.     public String getStringData() {
  101.         try{
  102.             return new String(this.resource,this.encoding);
  103.         }catch(Exception e){
  104.             throw new RuntimeException("Metodo getStringData() ha causato un errore",e);
  105.         }
  106.     }

  107.     @Override
  108.     public void setStringData(String stringData) {
  109.         this.resource = stringData.getBytes();
  110.     }

  111.     @Override
  112.     public String getSystemId() {
  113.         return this.systemId;
  114.     }

  115.     @Override
  116.     public void setSystemId(String systemId) {
  117.         this.systemId = systemId;
  118.     }

  119.     @Override
  120.     public String getPublicId() {
  121.         return this.publicId;
  122.     }

  123.     @Override
  124.     public void setPublicId(String publicId) {
  125.         this.publicId = publicId;
  126.     }

  127.     @Override
  128.     public String getBaseURI() {
  129.         return this.baseURI;
  130.     }

  131.     @Override
  132.     public void setBaseURI(String baseURI){
  133.         this.baseURI = baseURI;
  134.     }

  135.     @Override
  136.     public String getEncoding() {
  137.         return this.encoding;
  138.     }

  139.     @Override
  140.     public void setEncoding(String encoding) {
  141.         this.encoding = encoding;
  142.     }

  143.     @Override
  144.     public boolean getCertifiedText() {
  145.         throw new RuntimeException("Metodo getCertifiedText() non implementato");
  146.     }

  147.     @Override
  148.     public void setCertifiedText(boolean certifiedText) {
  149.         throw new RuntimeException("Metodo setCertifiedText(boolean certifiedText) non implementato");
  150.     }

  151.     public String getType() {
  152.         return this.type;
  153.     }

  154.     public void setType(String type) {
  155.         this.type = type;
  156.     }

  157.     public String getNamespaceURI() {
  158.         return this.namespaceURI;
  159.     }

  160.     public void setNamespaceURI(String namespaceURI) {
  161.         this.namespaceURI = namespaceURI;
  162.     }

  163.     public byte[] getResource() {
  164.         return this.resource;
  165.     }

  166.     public void setResource(byte[] resource) {
  167.         this.resource = resource;
  168.     }
  169. }