001package com.hfg.xml;
002
003import java.util.Map;
004import java.io.*;
005
006import com.hfg.exception.ProgrammingException;
007import com.hfg.util.StringUtil;
008import com.hfg.util.io.NoClosePrintWriter;
009
010//------------------------------------------------------------------------------
011/**
012 Generic container for XML processing instructions.
013
014 @author J. Alex Taylor, hairyfatguy.com
015 */
016//------------------------------------------------------------------------------
017// com.hfg XML/HTML Coding Library
018//
019// This library is free software; you can redistribute it and/or
020// modify it under the terms of the GNU Lesser General Public
021// License as published by the Free Software Foundation; either
022// version 2.1 of the License, or (at your option) any later version.
023//
024// This library is distributed in the hope that it will be useful,
025// but WITHOUT ANY WARRANTY; without even the implied warranty of
026// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
027// Lesser General Public License for more details.
028//
029// You should have received a copy of the GNU Lesser General Public
030// License along with this library; if not, write to the Free Software
031// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
032//
033// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
034// jataylor@hairyfatguy.com
035//------------------------------------------------------------------------------
036
037public class XMLProcessingInstruction implements XMLizable
038{
039   private String mTarget;
040   private String mData;
041
042   private static final String  NL = System.getProperty("line.separator");
043
044   //--------------------------------------------------------------------------
045   public XMLProcessingInstruction(String inTarget, String inData)
046   {
047      mTarget = inTarget;
048      mData   = inData;
049   }
050
051
052   //---------------------------------------------------------------------------
053   public XMLProcessingInstruction clone()
054   {
055      XMLProcessingInstruction cloneObj;
056      try
057      {
058         cloneObj = (XMLProcessingInstruction) super.clone();
059      }
060      catch (CloneNotSupportedException e)
061      {
062         throw new ProgrammingException(e);
063      }
064
065      return cloneObj;
066   }
067
068   //--------------------------------------------------------------------------
069   public boolean isEmptyTag()
070   {
071      return true;
072   }
073
074   //---------------------------------------------------------------------------
075   public void replaceCharacterEntities()
076   {
077
078   }
079
080   //---------------------------------------------------------------------------
081   public String toXML()
082   {
083      ByteArrayOutputStream outStream;
084      try
085      {
086         outStream = new ByteArrayOutputStream(2048);
087         toXML(outStream);
088         outStream.close();
089      }
090      catch (Exception e)
091      {
092         throw new XMLException(e);
093      }
094
095      return outStream.toString();
096   }
097
098   //---------------------------------------------------------------------------
099   public void toXML(OutputStream inStream)
100   {
101      PrintWriter writer = new PrintWriter(inStream);
102
103      toXML(writer);
104   }
105
106   //---------------------------------------------------------------------------
107   public void toXML(Writer inWriter)
108   {
109      try
110      {
111         inWriter.write("<?" + mTarget + " " + mData + "?>");
112      }
113      catch (IOException e)
114      {
115         throw new RuntimeException(e);
116      }
117   }
118
119   //---------------------------------------------------------------------------
120   public String toIndentedXML(int inInitialIndentLevel, int inIndentSize)
121   {
122      ByteArrayOutputStream outStream;
123      try
124      {
125         outStream = new ByteArrayOutputStream();
126         PrintWriter writer = new PrintWriter(outStream);
127         toIndentedXML(writer, inInitialIndentLevel, inIndentSize);
128         writer.close();
129      }
130      catch (Exception e)
131      {
132         throw new XMLException(e);
133      }
134
135      return outStream.toString();
136   }
137
138   //---------------------------------------------------------------------------
139   public void toIndentedXML(OutputStream inOutputStream, int inInitialIndentLevel, int inIndentSize)
140   {
141      PrintWriter writer = null;
142      try
143      {
144          writer = new NoClosePrintWriter(inOutputStream);
145          toIndentedXML(writer, inInitialIndentLevel, inIndentSize);
146      }
147      finally
148      {
149          if (writer != null) writer.close();
150      }
151   }
152
153   //---------------------------------------------------------------------------
154   public void toIndentedXML(Writer inWriter, int inInitialIndentLevel, int inIndentSize)
155   {
156      String indent = StringUtil.polyChar(' ', inInitialIndentLevel * inIndentSize);
157      try
158      {
159         if (inInitialIndentLevel > 0)
160         {
161            inWriter.write(NL);
162            inWriter.write(indent);
163         }
164
165         toXML(inWriter);
166      }
167      catch (IOException e)
168      {
169         throw new RuntimeException(e);
170      }
171   }
172
173   //---------------------------------------------------------------------------
174   public void toIndentedXML(Writer inWriter, int inInitialIndentLevel, int inIndentSize, Map<String, XMLNode> inNamespaceScopeMap)
175   {
176      toIndentedXML(inWriter, inInitialIndentLevel, inIndentSize);
177   }
178
179}