/*
* This software and supporting documentation were developed by
*
* Siemens Corporate Technology
* Competence Center Knowledge Management and Business Transformation
* D-81730 Munich, Germany
*
* Authors (representing a really great team ;-) )
* Stefan B. Augustin, Thorbj�rn Hansen, Manfred Langen
*
* This software is Open Source under GNU General Public License (GPL).
* Read the text of this license in LICENSE.TXT
* or look at www.opensource.org/licenses/
*
* Once more we emphasize, that:
* THIS SOFTWARE IS MADE AVAILABLE, AS IS, WITHOUT ANY WARRANTY
* REGARDING THE SOFTWARE, ITS PERFORMANCE OR
* FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR
* ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND
* PERFORMANCE OF THE SOFTWARE IS WITH THE USER.
*
*/
// XMLTranslatorBuilder
// ************ package ****************************************************
package appl.Portal.Utils.XML;
// ************ imports ******************************************************
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.io.StringWriter;
import java.util.Hashtable;
import java.net.URL;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
import appl.Portal.Utils.LinkSearch.*;
import KFM.Exceptions.*;
import KFM.Converter;
import KFM.log.*;
/**
* It implements this construction process by using the DOM API of the W3C.<P>
*
* For the DOM implementation, we use Xerces from the OpenXML project of Apache.
* It is freely available at http://www.openxml.org.
* The product chosen should not shine through at too many places, though.
* Where a product-specific statement is inevitable, we mark it as '//@XERCES'.
* A good idea would be to place product specific code (right now, this is only
* within method getXML()) into a specific subclass like XMLwithDOMBuilder_XERCES.<P>
*
* A small note: <BR>
* One small goal was to format output to comfortable readability even in a texteditor
* This couldn't be reached because the class "OutputFormat" understands only Indenting
* but no lineseparating
*
* @version 1.0 (2000-03-20), ready to use
* @see XMLBuilder
* @see ResultSet
* @see ResultItem
*/
public class XMLTranslatorBuilder implements GenericXMLBuilder
{
/**
* The fully qualified name of the class that implements interface org.w3c.dom.Document.
* Using this indirection, we manage to keep method build() portable to any
* other DOM-supporting product (in Java).
*/
static protected String mDocumentImplClass = "org.apache.xerces.dom.DocumentImpl";
/**
* The XML encoding to use.
*/
static protected String mEncoding = "ISO-8859-1";
/**
* The XMLDocument constructed.
*/
private Document mXMLDocument;
/**
* Log File
*/
private KFMLog mLog;
/**
* Construct an instance of XMLTranslatorBuilder.
*/
public XMLTranslatorBuilder(KFMLog aLog)
{
mLog=aLog;
}
/**
* Construct an instance of XMLTranslatorBuilder.
*/
public XMLTranslatorBuilder()
{
mLog=KFMSystem.log;
}
/**
* Construct the XML for the set of Links that is described by a ResultSet.
* The result is 'memorized', so that a subsequent call of getXML()
* can return the XML document.
*
* @param aResultSet the ResultSet describing all the Links.
*/
public void build( GenericResultSet aResultSet )
{
String tAttrValue;
Hashtable tIdiom;
int tItemCount = 0;
try
{
// create the XML document
//========================
mXMLDocument = ( Document ) Class.forName( mDocumentImplClass ).newInstance();
// create XML element 'ResultSet' as the root of the XML document
//===============================================================
Element wordpairSet = mXMLDocument.createElement( "WordpairSet" );
mXMLDocument.appendChild( wordpairSet );
// set Attributes of XML element 'ResultSet'
//==========================================
while( aResultSet.hasMoreItems() )
{
tItemCount++;
// This loop iterates as long as there are resultitems available
// for every Iteration one Result Element is defined.
tIdiom = aResultSet.nextItem();
// create next XML element 'Result' as a child of XML element 'ResultSet'
//=======================================================================
Element wordpair = mXMLDocument.createElement( "Wordpair" );
wordpairSet.appendChild( wordpair );
// set Attributes of XML element 'Result'
//=======================================
tAttrValue = (String)tIdiom.get("en");
if( tAttrValue != null )
{
Element englishWord = mXMLDocument.createElement
( "EnglishWord" );
englishWord.appendChild( mXMLDocument.createTextNode( tAttrValue ));
wordpair.appendChild( englishWord);
}
tAttrValue = (String)tIdiom.get("de");
if( tAttrValue != null )
{
Element germanWord = mXMLDocument.createElement
( "GermanWord" );
germanWord.appendChild( mXMLDocument.createTextNode( tAttrValue ));
wordpair.appendChild( germanWord);
}
}
// some final Attribute of XML element 'ResultSet' is the number of results
// (we only know the value after the loop)
wordpairSet.setAttribute( "NumResults", String.valueOf(tItemCount) );
}
catch( Exception e)
{
mLog.error("Error while creating XML document", e);
}
}
/**
* Return the XML document as a String.
*
* @return the XML document as a String
*/
public String getXML()
{
StringWriter tXMLWriter = new StringWriter();
try
{
// a serializer
OutputFormat outputFormat = new OutputFormat( mXMLDocument, null, true ); //@XERCES
outputFormat.setEncoding(mEncoding); //@XERCES
XMLSerializer ser = new XMLSerializer( tXMLWriter, outputFormat ); //@XERCES
ser.serialize( mXMLDocument ); //@XERCES
}
catch( IOException e )
{
mLog.error("getXML returns IOException", e);
}
return tXMLWriter.toString();
}
/**
* Return the XML document as a DOM object.
*
* @return the XML document as a DOM object
*/
public Document getXMLasDOM()
{
return mXMLDocument;
}
/**
* A helper method to quote an attribute value.
* This ensures that &, <, >, are not included inside XML attribute values.
* This method delegates to Converter#quoteHtml().
*/
protected String quote (String anAttributeValue)
{
return Converter.quoteHtml(anAttributeValue);
}
}