XMLRootElement.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.protocol.sdk;

  21. import java.io.Serializable;

  22. /**
  23.  * Bean Contenente le informazioni per il root element di elementi xml
  24.  *
  25.  * @author Andrea Poli (apoli@link.it)
  26.  * @author $Author$
  27.  * @version $Rev$, $Date$
  28.  *
  29.  */
  30. public abstract class XMLRootElement implements Serializable{

  31.     private static final long serialVersionUID = -3157816024001587816L;

  32.     private String localName;
  33.     private String namespace;
  34.     private String prefix;
  35.    
  36.     protected XMLRootElement(String localName,String namespace,String prefix){
  37.         this.localName = localName;
  38.         this.namespace = namespace;
  39.         this.prefix = prefix;
  40.     }
  41.     protected XMLRootElement(String localName,String namespace){
  42.         this(localName, namespace, null);
  43.     }
  44.     protected XMLRootElement(){}
  45.    
  46.     public String getLocalName() {
  47.         return this.localName;
  48.     }
  49.     public void setLocalName(String localName) {
  50.         this.localName = localName;
  51.     }
  52.     public String getNamespace() {
  53.         return this.namespace;
  54.     }
  55.     public void setNamespace(String namespace) {
  56.         this.namespace = namespace;
  57.     }
  58.     public String getPrefix() {
  59.         return this.prefix;
  60.     }
  61.     public void setPrefix(String prefix) {
  62.         this.prefix = prefix;
  63.     }

  64.     public String getAsStringStartTag()throws ProtocolException{
  65.         return this.getAsStringTag(true);
  66.     }
  67.     public String getAsStringEndTag()throws ProtocolException{
  68.         return this.getAsStringTag(false);
  69.     }
  70.     private String getAsStringTag(boolean start) throws ProtocolException{
  71.        
  72.         if(this.localName==null || "".equals(this.localName)){
  73.             throw new ProtocolException("LocalName not defined");
  74.         }
  75.         if(this.namespace==null || "".equals(this.namespace)){
  76.             throw new ProtocolException("Namespace not defined");
  77.         }
  78.        
  79.         StringBuilder bf = new StringBuilder();
  80.         bf.append("<");
  81.         if(!start){
  82.             bf.append("/");
  83.         }
  84.         if(this.prefix!=null && !"".equals(this.prefix)){
  85.             bf.append(this.prefix).append(":");
  86.         }
  87.         bf.append(this.localName);
  88.         if(start){
  89.             bf.append(" xmlns");
  90.             if(this.prefix!=null && !"".equals(this.prefix)){
  91.                 bf.append(":").append(this.prefix);
  92.             }
  93.             bf.append("=\"").append(this.namespace).append("\"");
  94.         }
  95.         bf.append(">");
  96.         return bf.toString();
  97.     }
  98. }