All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.xml.XMLProcessingInstruction Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.xml;

import java.util.Map;
import java.io.*;

import com.hfg.exception.ProgrammingException;
import com.hfg.util.StringUtil;
import com.hfg.util.io.NoClosePrintWriter;

//------------------------------------------------------------------------------
/**
 Generic container for XML processing instructions.

 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class XMLProcessingInstruction implements XMLizable
{
   private String mTarget;
   private String mData;

   private static final String  NL = System.getProperty("line.separator");

   //--------------------------------------------------------------------------
   public XMLProcessingInstruction(String inTarget, String inData)
   {
      mTarget = inTarget;
      mData   = inData;
   }


   //---------------------------------------------------------------------------
   public XMLProcessingInstruction clone()
   {
      XMLProcessingInstruction cloneObj;
      try
      {
         cloneObj = (XMLProcessingInstruction) super.clone();
      }
      catch (CloneNotSupportedException e)
      {
         throw new ProgrammingException(e);
      }

      return cloneObj;
   }

   //--------------------------------------------------------------------------
   public boolean isEmptyTag()
   {
      return true;
   }

   //---------------------------------------------------------------------------
   public void replaceCharacterEntities()
   {

   }

   //---------------------------------------------------------------------------
   public String toXML()
   {
      ByteArrayOutputStream outStream;
      try
      {
         outStream = new ByteArrayOutputStream(2048);
         toXML(outStream);
         outStream.close();
      }
      catch (Exception e)
      {
         throw new XMLException(e);
      }

      return outStream.toString();
   }

   //---------------------------------------------------------------------------
   public void toXML(OutputStream inStream)
   {
      PrintWriter writer = new PrintWriter(inStream);

      toXML(writer);
   }

   //---------------------------------------------------------------------------
   public void toXML(Writer inWriter)
   {
      try
      {
         inWriter.write("");
      }
      catch (IOException e)
      {
         throw new RuntimeException(e);
      }
   }

   //---------------------------------------------------------------------------
   public String toIndentedXML(int inInitialIndentLevel, int inIndentSize)
   {
      ByteArrayOutputStream outStream;
      try
      {
         outStream = new ByteArrayOutputStream();
         PrintWriter writer = new PrintWriter(outStream);
         toIndentedXML(writer, inInitialIndentLevel, inIndentSize);
         writer.close();
      }
      catch (Exception e)
      {
         throw new XMLException(e);
      }

      return outStream.toString();
   }

   //---------------------------------------------------------------------------
   public void toIndentedXML(OutputStream inOutputStream, int inInitialIndentLevel, int inIndentSize)
   {
      PrintWriter writer = null;
      try
      {
          writer = new NoClosePrintWriter(inOutputStream);
          toIndentedXML(writer, inInitialIndentLevel, inIndentSize);
      }
      finally
      {
          if (writer != null) writer.close();
      }
   }

   //---------------------------------------------------------------------------
   public void toIndentedXML(Writer inWriter, int inInitialIndentLevel, int inIndentSize)
   {
      String indent = StringUtil.polyChar(' ', inInitialIndentLevel * inIndentSize);
      try
      {
         if (inInitialIndentLevel > 0)
         {
            inWriter.write(NL);
            inWriter.write(indent);
         }

         toXML(inWriter);
      }
      catch (IOException e)
      {
         throw new RuntimeException(e);
      }
   }

   //---------------------------------------------------------------------------
   public void toIndentedXML(Writer inWriter, int inInitialIndentLevel, int inIndentSize, Map inNamespaceScopeMap)
   {
      toIndentedXML(inWriter, inInitialIndentLevel, inIndentSize);
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy