/********************************************************* begin of preamble
**
** Copyright (C) 2003-2010 Software- und Organisations-Service GmbH.
** All rights reserved.
**
** This file may be used under the terms of either the
**
** GNU General Public License version 2.0 (GPL)
**
** as published by the Free Software Foundation
** http://www.gnu.org/licenses/gpl-2.0.txt and appearing in the file
** LICENSE.GPL included in the packaging of this file.
**
** or the
**
** Agreement for Purchase and Licensing
**
** as offered by Software- und Organisations-Service GmbH
** in the respective terms of supply that ship with this file.
**
** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
** THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
** PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
** BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
** CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
** SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
** INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
** CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
** ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
********************************************************** end of preamble*/
package com.sos.JSHelper.io.Files;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Vector;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import com.sos.JSHelper.Options.SOSOptionFileName;
/* ---------------------------------------------------------------------------
APL/Software GmbH - Berlin
##### generated by ClaviusXPress (http://www.sos-berlin.com) #########
2007-09-10, kb@sos-berlin.com (sgx2343)
-------------------------------------------------------------------------------
<docu type="smcw" version="1.0">
<project>com.sos.IDocs</project>
<name>JSXMLFile</name>
<title>JSXMLFile
</title>
<description>
<para>
JSXMLFile
</para>
</description>
<params>
</params>
<keywords>
<keyword>IDoc</keyword>
<keyword>Splitter</keyword>
</keywords>
<categories>
<category>IDoc</category>
</categories>
<date>2007-09-10</date>
<copyright>� 2006 by Klaus D. Buettner</copyright>
<author>kb@sos-berlin.com</author>
<changes>
<change who='sgx2343' when='2007-09-10' id='created'>
<what>
<para>
created
</para>
</what>
</change>
</changes>
</docu>
---------------------------------------------------------------------------- */
public class JSXMLFile extends JSTextFile /* implements ErrorHandler */ {
private static final long serialVersionUID = 1L;
private final String conClassName = "JSXMLFile";
protected final String conMissingDate = "0000-00-00";
protected final String conMissingDate2 = "--";
private Boolean flgFileIsOpen = false;
private int intIndent = 0;
private Boolean flgDecrIndent = false;
private Boolean flgIncrIndent = false;
private Vector<SOSXMLAttribute> lstAttributes = null;
private int intNumberOfXMLDeclarations = 0;
public JSXMLFile() {
super(null);
// nothing to do
}
public JSXMLFile (final SOSOptionFileName pobjOptionElementFileName) throws Exception {
super(pobjOptionElementFileName.Value());
strFileName = pobjOptionElementFileName.Value();
}
public JSXMLFile(final String pstrFileName) throws Exception {
super(pstrFileName);
strFileName = pstrFileName;
}
/* ---------------------------------------------------------------------------
* <br />
* Create XML-File for IDoc
*
*/
/*
<method type="smcw" version="1.0">
<name>CreateXMLFile</name>
<title>Create XML-File for IDoc</title>
<description>
<para>
Create XML-File for IDoc
</para>
<para>
Die Methode legt den XML-File an.
Falls er bereits existiert, wird er zun�chst gel�scht und dann neu angelegt.
</para>
<para>
Die Namespace-Infos werden aus den IDoc-Optionen �bernommen.
</para>
<para>
Der Tag "TechnicalInformations" mit den Untertags wird angelegt.
Die notwendigen Infos werden den IDoc-Optionen entnommen.
</para>
</description>
<params>
</params>
<keywords>
<keyword>IDoc</keyword>
<keyword>XML-File</keyword>
</keywords>
<categories>
<category>IDoc</category>
<category>XML</category>
</categories>
</method>
---------------------------------------------------------------------------- */
public void open(@SuppressWarnings("unused")
final
String pstrOutputFileName) throws Exception {
@SuppressWarnings("unused")
final String conMethodName = conClassName + "::open";
// if (FileHandle == null) {
// FileWriter fleFileWriter = null;
// strFileName = pstrOutputFileName;
// fleFile = new File(strFileName);
//
// if (fleFile.exists()) {
// Date dteLastModified = new Date(fleFile.lastModified());
// message(conMethodName + " - File exist: " + strFileName + ", Size is : " + fleFile.length() + ", last modified : " + dteLastModified);
// fleFile.delete();
// fleFileWriter = new FileWriter(fleFile);
// }
// else {
// fleFileWriter = new FileWriter(fleFile);
// }
// FileHandle = new BufferedWriter(fleFileWriter);
// message(conMethodName + " - File opened: " + strFileName);
// }
// newTag(pobjIDocOptions.RootTagName() + " " + pobjIDocOptions.xmlns());
// CreateTechnicalInformations(pobjIDocOptions);
}
public JSXMLFile XMLTagV(final String pstrTagName, final String pstrTagValue) throws Exception {
return this.newTag(pstrTagName, pstrTagValue);
}
private String getStartTag(final String strTagName) {
final String strT = addBraces(adjustTagName(strTagName) + BuildAttributesString());
if (strTagName.startsWith("/")) {
flgDecrIndent = true;
}
else {
flgIncrIndent = true;
}
return strT;
}
private String BuildAttributesString() {
String strT = "";
if (lstAttributes != null) {
for (int i = 0; i < lstAttributes.size(); i++) {
strT += " " + lstAttributes.elementAt(i).toString();
}
lstAttributes = null;
}
return strT;
}
private String adjustTagName(final String pstrTagName) {
return pstrTagName;
}
private String addBraces(final String pstrParam) {
return "<" + pstrParam + ">";
}
private String getEndTag(final String strTagName) {
flgDecrIndent = true;
final String strT = addBraces("/" + adjustTagName(strTagName));
return strT;
}
/**
*
* \brief tag
*
* \details
*
* \return String
*
* @param pstrTagName
* @param pstrTagValue
* @return
*/
public String tag(final String pstrTagName, final String pstrTagValue) {
@SuppressWarnings("unused")
final String conMethodName = conClassName + "::tag";
String strValue = pstrTagValue;
if (strValue == null) {
strValue = "";
}
else {
if (strValue.matches(".*[&<>].*") && !pstrTagValue.contains("![CDATA[")) {
strValue = MakeCData(strValue);
}
}
final String strTemp = String.format("%1$s%2$s%3$s", getStartTag(pstrTagName), strValue, getEndTag(pstrTagName));
return strTemp;
} // public String tag
public JSXMLFile newTag(final String pstrTagName, final String pstrTagValue) throws Exception {
String strValue = pstrTagValue;
if (strValue == null) {
strValue = "";
}
else {
//TODO �nderung pr�fen
// if (strValue.matches(".*[&<>].*")) {
if (strValue.matches(".*[&<>].*") && !pstrTagValue.contains("![CDATA[")) {
strValue = MakeCData(strValue);
}
}
Write2File(String.format("%1$s%2$s%3$s", getStartTag(pstrTagName), strValue, getEndTag(pstrTagName)));
return this;
}
public JSXMLFile Write2File(final String pstrS) throws Exception {
String strT = "";
if (!flgFileIsOpen) {
flgFileIsOpen = true;
// TODO version und encoding �ber Options steuern.
}
if (flgIncrIndent) {
strT = getIndent();
intIndent++;
flgIncrIndent = false;
}
if (flgDecrIndent) {
intIndent--;
flgDecrIndent = false;
strT = getIndent();
}
super.Write(strT + pstrS);
return this;
}
private String getIndent() {
String strT = "";
if (intIndent > 0) {
strT = "";
for (int i = 0; i < intIndent; i++) {
strT += "\t"; // " ";
}
}
return strT;
}
/**
*
* \brief writeXMLDeclaration
*
* \details
*
* \return JSXMLFile
*
* @param pstrCharset
* @return
* @throws Exception
*/
public JSXMLFile writeXMLDeclaration(final String pstrCharset) throws Exception {
@SuppressWarnings("unused")
final String conMethodName = conClassName + "::writeXMLDeclaration";
String strCharset = pstrCharset;
if (strCharset == null || strCharset.length() <= 0) {
strCharset = "ISO-8859-1";
}
Write2File(addBraces("?xml version='1.0' encoding='" + strCharset + "' ?"));
//- <remark who='EQALS' when='Dienstag, 20. Oktober 2009' id='Korrektur' >
/**
* \change Dienstag, 20. Oktober 2009 EQALS Korrektur
* Korrektur: Wenn mehrere XML Deklarationen, dann nicht validieren
*/
//- <newcode>
intNumberOfXMLDeclarations++;
//- </newcode>
//- </remark> <!-- id=<Korrektur> -->
return this;
} // public JSXMLFile writeXMLDeclaration}
public JSXMLFile writeXMLDeclaration() throws Exception {
writeXMLDeclaration(null);
return this;
}
public JSXMLFile comment(final String pstrComment) throws Exception {
Write2File(addBraces("!-- " + pstrComment + " --"));
return this;
}
/**
*
* \brief tag
*
* \details
*
* \return String
*
* @param pstrTagName
* @return
*/
public String tag(final String pstrTagName) {
@SuppressWarnings("unused")
final String conMethodName = conClassName + "::tag";
return getStartTag(pstrTagName);
} // public String tag}
public JSXMLFile newTag(final String pstrTagName) throws Exception {
Write2File(getStartTag(pstrTagName));
return this;
}
public JSXMLFile endTag(final String pstrTagName) throws Exception {
Write2File(getEndTag(pstrTagName));
return this;
}
public JSXMLFile newCDataTag(final String pstrTagName) throws Exception {
Write2File(getStartTag(pstrTagName) + "<![CDATA[");
return this;
}
public JSXMLFile endCDataTag(final String pstrTagName) throws Exception {
Write2File("]]>" + getEndTag(pstrTagName));
return this;
}
public JSXMLFile newCDataTag(final String pstrTagName, final String pstrTagValue) throws Exception {
if (pstrTagValue != null && pstrTagName != null) {
if (pstrTagValue.length() > 0) {
this.newTag(pstrTagName, MakeCData(pstrTagValue));
}
}
return this;
}
/**
*
* \brief MakeCData
*
* \details
*
* \return String
*
* @param pstrValue
* @return
*/
public String MakeCData(final String pstrValue) {
return addBraces("![CDATA[" + pstrValue + "]]");
}
public JSXMLFile newDateTag(final String pstrTagName, final String pstrTagValue) throws Exception {
// if (pstrTagValue != null && pstrTagValue.equals(conMissingDate)) {
if (pstrTagValue != null && (pstrTagValue.equals(conMissingDate) || pstrTagValue.equals("") || pstrTagValue.trim().equals(conMissingDate2))) {
// nothing to do; tag omitted
}
else {
this.newTag(pstrTagName, pstrTagValue);
}
return this;
}
// public void Close() throws IOException {
//
// @SuppressWarnings("unused")
// final String conMethodName = conClassName + "::Close";
//
// super.close();
// }
public JSXMLFile addAttribute(final String pstrAttributeName, final String pstrAttributeValue) {
final SOSXMLAttribute objAtt = new SOSXMLAttribute(pstrAttributeName, pstrAttributeValue);
if (lstAttributes == null) {
lstAttributes = new Vector<SOSXMLAttribute>();
}
lstAttributes.add(objAtt);
return this;
} // public
/**
*
* \brief Transform
*
* \details
*
* \return void
*
* @param xslFile
* @param outputFile
* @throws TransformerException
* @throws TransformerConfigurationException
* @throws FileNotFoundException
* @throws Exception
*/
public void Transform(final File xslFile, final File outputFile) throws TransformerException,
TransformerConfigurationException, FileNotFoundException, Exception {
final TransformerFactory tFactory = TransformerFactory.newInstance();
final Transformer transformer = tFactory.newTransformer(new StreamSource(xslFile));
if (outputFile == null) {
transformer.transform(new StreamSource(this), // ...
new StreamResult(new java.io.OutputStreamWriter(System.out)));
}
else {
// final boolean append = true;
transformer.transform(new StreamSource(this), // ...
new StreamResult(new FileOutputStream(outputFile)));
}
}
/**
*
* \brief Validate
*
* \details
*
* \return void
*
* @throws SAXException
* @throws IOException
*/
public void Validate() throws SAXException, IOException {
@SuppressWarnings("unused")
final String conMethodName = conClassName + "::Validate";
//- <remark who='EQALS' when='Dienstag, 20. Oktober 2009' id='Korrektur' >
/**
* \change Dienstag, 20. Oktober 2009 EQALS Korrektur
* Korrektur: Wenn mehrere XML Deklarationen, dann nicht validieren
*/
//- <newcode>
if(intNumberOfXMLDeclarations > 1) {
return;
}
//- </newcode>
//- </remark> <!-- id=<Korrektur> -->
// final SAXParser parser = new SAXParser(); // Xcerces
// parser.setFeature("http://xml.org/sax/features/validation", true);
// Set Handlers in the parser
// parser.setContentHandler(sample);
// parser.setEntityResolver(sample);
// parser.setDTDHandler(sample);
// parser.setErrorHandler(this);
// Convert file to URL and parse
//spooler_log.info("createURL(xmlFileName): " + createURL(xmlFileName));
//parser.parse(createURL(xmlFileName).toString());
// parser.parse(new org.xml.sax.InputSource(super.Reader()));
// return void;
} // public void Validate
/**
*
* @author eqbfd
*
*/
class SOSXMLAttribute {
private String strAttributeName = null;
private String strAttributeValue = null;
public SOSXMLAttribute() {
super();
// TODO Auto-generated constructor stub
}
public SOSXMLAttribute(final String pstrAttributeName, final String pstrAttributeValue) {
strAttributeName = pstrAttributeName;
strAttributeValue = pstrAttributeValue;
}
@Override
public String toString() {
return strAttributeName + "='" + strAttributeValue + "'";
}
} // class SOSXMLAttribute
class SOSXMLTag {
private String Name;
private String Value;
private Boolean isCdata;
private int DataType;
public SOSXMLTag() {
//
}
@Override
public String toString() {
//
return "not implemented";
}
public SOSXMLTag(final String pstrTagName) {
this(pstrTagName, "");
}
public SOSXMLTag(final String pstrTagName, final String pstrTagValue) {
Name = pstrTagName;
Value = pstrTagValue;
}
/**
* @return Returns the dataType.
*/
public int getDataType() {
return DataType;
}
/**
* @param dataType The dataType to set.
*/
public void setDataType(final int dataType) {
DataType = dataType;
}
/**
* @return Returns the isCdata.
*/
public Boolean getIsCdata() {
return isCdata;
}
/**
* @param isCdata The isCdata to set.
*/
public void setIsCdata(final Boolean isCdata) {
this.isCdata = isCdata;
}
/**
* @return Returns the name.
*/
public String getName() {
return Name;
}
/**
* @param name The name to set.
*/
public void setName(final String name) {
Name = name;
}
/**
* @return Returns the value.
*/
public String getValue() {
return Value;
}
/**
* @param value The value to set.
*/
public void setValue(final String value) {
Value = value;
}
} // class SOSXMLTag
public void error(final SAXParseException pobjException) throws SAXException {
pobjException.printStackTrace();
throw pobjException;
}
public void fatalError(final SAXParseException arg0) throws SAXException {
}
public void warning(final SAXParseException arg0) throws SAXException {
}
} // JSXMLFile