diff --git a/icu4c/source/samples/all/all.dsp b/icu4c/source/samples/all/all.dsp new file mode 100644 index 00000000000..6d4a33be570 --- /dev/null +++ b/icu4c/source/samples/all/all.dsp @@ -0,0 +1,85 @@ +# Microsoft Developer Studio Project File - Name="all" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Console Application" 0x0103 + +CFG=all - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "all.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "all.mak" CFG="all - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "all - Win32 Release" (based on "Win32 (x86) Console Application") +!MESSAGE "all - Win32 Debug" (based on "Win32 (x86) Console Application") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +RSC=rc.exe + +!IF "$(CFG)" == "all - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c +# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /c +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 + +!ELSEIF "$(CFG)" == "all - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c +# ADD CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /YX /FD /GZ /c +# ADD BASE RSC /l 0x409 /d "_DEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept + +!ENDIF + +# Begin Target + +# Name "all - Win32 Release" +# Name "all - Win32 Debug" +# End Target +# End Project diff --git a/icu4c/source/samples/all/all.dsw b/icu4c/source/samples/all/all.dsw new file mode 100644 index 00000000000..d268bb49a17 --- /dev/null +++ b/icu4c/source/samples/all/all.dsw @@ -0,0 +1,287 @@ +Microsoft Developer Studio Workspace File, Format Version 6.00 +# WARNING: DO NOT EDIT OR DELETE THIS WORKSPACE FILE! + +############################################################################### + +Project: "all"=.\all.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ + Begin Project Dependency + Project_Dep_Name cal + End Project Dependency + Begin Project Dependency + Project_Dep_Name date + End Project Dependency + Begin Project Dependency + Project_Dep_Name datefmt + End Project Dependency + Begin Project Dependency + Project_Dep_Name layout + End Project Dependency + Begin Project Dependency + Project_Dep_Name legacy + End Project Dependency + Begin Project Dependency + Project_Dep_Name msgfmt + End Project Dependency + Begin Project Dependency + Project_Dep_Name numfmt + End Project Dependency + Begin Project Dependency + Project_Dep_Name props + End Project Dependency + Begin Project Dependency + Project_Dep_Name reader + End Project Dependency + Begin Project Dependency + Project_Dep_Name resources + End Project Dependency + Begin Project Dependency + Project_Dep_Name scsu + End Project Dependency + Begin Project Dependency + Project_Dep_Name search + End Project Dependency + Begin Project Dependency + Project_Dep_Name translit + End Project Dependency + Begin Project Dependency + Project_Dep_Name ucnv + End Project Dependency + Begin Project Dependency + Project_Dep_Name ufortune + End Project Dependency + Begin Project Dependency + Project_Dep_Name uresb + End Project Dependency + Begin Project Dependency + Project_Dep_Name ustring + End Project Dependency + Begin Project Dependency + Project_Dep_Name writer + End Project Dependency +}}} + +############################################################################### + +Project: "cal"=..\cal\cal.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "date"=..\date\date.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "datefmt"=..\datefmt\datefmt.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "layout"=..\layout\layout.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "legacy"=..\legacy\legacy.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "msgfmt"=..\msgfmt\msgfmt.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "numfmt"=..\numfmt\numfmt.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "props"=..\props\props.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "reader"=..\udata\reader.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "resources"=..\uresb\resources.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "translit"=..\translit\translit.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "ucnv"=..\ucnv\ucnv.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "ufortune"=..\ufortune\ufortune.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "uresb"=..\uresb\uresb.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "ustring"=..\ustring\ustring.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "writer"=..\udata\writer.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Project: "xml2txt"=..\xml2txt\xml2txt.dsp - Package Owner=<4> + +Package=<5> +{{{ +}}} + +Package=<4> +{{{ +}}} + +############################################################################### + +Global: + +Package=<5> +{{{ +}}} + +Package=<3> +{{{ +}}} + +############################################################################### + diff --git a/icu4c/source/samples/xml2txt/ChildName.cpp b/icu4c/source/samples/xml2txt/ChildName.cpp new file mode 100644 index 00000000000..eccceaaabaa --- /dev/null +++ b/icu4c/source/samples/xml2txt/ChildName.cpp @@ -0,0 +1,10 @@ +#include "ChildName.h" + +void ChildName ::SetName(DOMString name) +{ + Name = name; +}; +void ChildName::SetNext(ChildName* next) +{ + Next = next; +}; \ No newline at end of file diff --git a/icu4c/source/samples/xml2txt/ChildName.h b/icu4c/source/samples/xml2txt/ChildName.h new file mode 100644 index 00000000000..bbce622d993 --- /dev/null +++ b/icu4c/source/samples/xml2txt/ChildName.h @@ -0,0 +1,25 @@ +#ifndef CHILDNAME_H_ +#define CHILDNAME_H_ +#include +#include +#include +#include +#include +#include +#include +#include +#include "DOMTreeErrorReporter.hpp" + +class ChildName +{ + public: + DOMString Name; + ChildName* Next; + + ChildName(){}; + + void SetName(DOMString name); + void SetNext(ChildName* next); +}; + +#endif \ No newline at end of file diff --git a/icu4c/source/samples/xml2txt/DOMPrintFormatTarget.cpp b/icu4c/source/samples/xml2txt/DOMPrintFormatTarget.cpp new file mode 100644 index 00000000000..718ef2c5503 --- /dev/null +++ b/icu4c/source/samples/xml2txt/DOMPrintFormatTarget.cpp @@ -0,0 +1,19 @@ +#include "DOMPrintFormatTarget.h" + +DOMPrintFormatTarget::DOMPrintFormatTarget() { + +}; + +DOMPrintFormatTarget::DOMPrintFormatTarget(char* fileName) { + this->fileName = fileName; +} + +DOMPrintFormatTarget::~DOMPrintFormatTarget() {}; + +void DOMPrintFormatTarget :: writeChars(const XMLByte* const toWrite, + const unsigned int count, + XMLFormatter * const formatter) +{ + ofstream ofile( fileName, ios::app); + ofile.write((char *) toWrite, (int) count); +}; \ No newline at end of file diff --git a/icu4c/source/samples/xml2txt/DOMPrintFormatTarget.h b/icu4c/source/samples/xml2txt/DOMPrintFormatTarget.h new file mode 100644 index 00000000000..6db6f280cb7 --- /dev/null +++ b/icu4c/source/samples/xml2txt/DOMPrintFormatTarget.h @@ -0,0 +1,29 @@ +#ifndef DOMPRINTFORMATTARGET_H_ +#define DOMPRINTFORMATTARGET_H_ +#include "ChildName.h" +#include +class DOMPrintFormatTarget : public XMLFormatTarget +{ +private: + char* fileName; +public: + DOMPrintFormatTarget(); + DOMPrintFormatTarget(char* fileName); + ~DOMPrintFormatTarget(); + // ----------------------------------------------------------------------- + // Implementations of the format target interface + // ----------------------------------------------------------------------- + + void writeChars(const XMLByte* const toWrite, + const unsigned int count, + XMLFormatter * const formatter); + +private: + // ----------------------------------------------------------------------- + // Unimplemented methods. + // ----------------------------------------------------------------------- + DOMPrintFormatTarget(const DOMPrintFormatTarget& other); + void operator=(const DOMPrintFormatTarget& rhs); +}; + +#endif \ No newline at end of file diff --git a/icu4c/source/samples/xml2txt/DOMTreeErrorReporter.cpp b/icu4c/source/samples/xml2txt/DOMTreeErrorReporter.cpp new file mode 100644 index 00000000000..1d6eb7f131c --- /dev/null +++ b/icu4c/source/samples/xml2txt/DOMTreeErrorReporter.cpp @@ -0,0 +1,37 @@ +// --------------------------------------------------------------------------- +// Includes +// --------------------------------------------------------------------------- +#include +#include "DOMTreeErrorReporter.hpp" +#include +#include +#include +#include + +extern ostream& operator<<(ostream& target, const DOMString& s); + +void DOMTreeErrorReporter::warning(const SAXParseException&) +{ +} + +void DOMTreeErrorReporter::error(const SAXParseException& toCatch) +{ + fSawErrors = true; + cerr << "Error at file \"" << DOMString(toCatch.getSystemId()) + << "\", line " << toCatch.getLineNumber() + << ", column " << toCatch.getColumnNumber() + << "\n Message: " << DOMString(toCatch.getMessage()) << endl; +} + +void DOMTreeErrorReporter::fatalError(const SAXParseException& toCatch) +{ + fSawErrors = true; + cerr << "Fatal Error at file \"" << DOMString(toCatch.getSystemId()) + << "\", line " << toCatch.getLineNumber() + << ", column " << toCatch.getColumnNumber() + << "\n Message: " << DOMString(toCatch.getMessage()) << endl; +} + +void DOMTreeErrorReporter::resetErrors() +{ +} diff --git a/icu4c/source/samples/xml2txt/DOMTreeErrorReporter.hpp b/icu4c/source/samples/xml2txt/DOMTreeErrorReporter.hpp new file mode 100644 index 00000000000..6305874aeb9 --- /dev/null +++ b/icu4c/source/samples/xml2txt/DOMTreeErrorReporter.hpp @@ -0,0 +1,47 @@ +#include +#include +#include + +class DOMTreeErrorReporter : public ErrorHandler +{ +public: + // ----------------------------------------------------------------------- + // Constructors and Destructor + // ----------------------------------------------------------------------- + DOMTreeErrorReporter() : + fSawErrors(false) + { + } + + ~DOMTreeErrorReporter() + { + } + + // ----------------------------------------------------------------------- + // Implementation of the error handler interface + // ----------------------------------------------------------------------- + void warning(const SAXParseException& toCatch); + void error(const SAXParseException& toCatch); + void fatalError(const SAXParseException& toCatch); + void resetErrors(); + + // ----------------------------------------------------------------------- + // Getter methods + // ----------------------------------------------------------------------- + bool getSawErrors() const; + + // ----------------------------------------------------------------------- + // Private data members + // + // fSawErrors + // This is set if we get any errors, and is queryable via a getter + // method. Its used by the main code to suppress output if there are + // errors. + // ----------------------------------------------------------------------- + bool fSawErrors; +}; + +inline bool DOMTreeErrorReporter::getSawErrors() const +{ + return fSawErrors; +} \ No newline at end of file diff --git a/icu4c/source/samples/xml2txt/xml2res.pl b/icu4c/source/samples/xml2txt/xml2res.pl new file mode 100755 index 00000000000..125779c5655 --- /dev/null +++ b/icu4c/source/samples/xml2txt/xml2res.pl @@ -0,0 +1,124 @@ +#/usr/bin/perl + +#################################################################################### +# xml2res.pl: +# This tool invokes xml2txt and genrb to produce res files from xml files +# Author: Ram Viswanadha +# +#################################################################################### +use File::Find; +use File::Basename; +use IO::File; +use Cwd; +use File::Copy; +use Getopt::Long; +use File::Path; +use File::Copy; + +GetOptions( + "--lib=s" => \$envVar, + "--icuroot=s" => \$icuRoot, + "--xerces=s" => \$xercesBin, + "--xml2txt=s" => \$xml2txt, + "--genrb=s" => \$genrb, + "--sourcedir=s" => \$sourceDir, + "--destdir=s" => \$destDir); + + +usage() unless defined $icuRoot; +usage() unless defined $xercesBin; +usage() unless defined $sourceDir; +usage() unless defined $destDir; +usage() unless defined $xml2txt; +usage() unless defined $genrb; + +# create a temp directory and copy all the txt files there +my $tempDir = $destDir."/temp"; +mkpath($tempDir); +my $prefix; + +# set up environment +if($$^O =~ /win/){ + $prefix =""; + cmd("set PATH=%PATH%;$icuRoot/bin;$xercesBin;"); +}else{ + $prefix ="$ldVar=$ICU_ROOT/source/common:$ICU_ROOT/source/i18n:$ICU_ROOT/source/tools/toolutil:$ICU_ROOT/source/data/out:$ICU_ROOT/source/data: " +} + +# create list of xml files +my @list; +if (@ARGV) { + @list = @ARGV; + foreach (@list) { $_ .= ".xml" unless (/\.xml$/i); } +} else { + opendir(DIR,$sourceDir); + @list = grep{/\.xml$/} readdir(DIR); + closedir(DIR); +} + +# now convert +foreach $item (@list){ + next if($item eq "." || $item eq ".."); + texify($item); + $txt = $item;; + $txt =~ s/xml$/txt/i; + resify($txt); +} + +# run the xml2txt converter +sub texify{ + my $infile = shift; + my $xml2txtExec = $xml2txt."/xml2txt"; + cmd("$prefix $xml2txtExec --sourcedir $sourceDir --destdir $tempDir $infile"); +} + +# run genrb +sub resify{ + my $infile = shift; + my $genrbExec = $genrb."/genrb"; + cmd("$prefix $genrbExec --sourcedir $tempDir --destdir $destDir --encoding UTF8 $infile"); +} + +#----------------------------------------------------------------------- +# Execute a command +# Param: Command +# Param: Display line, or '' to display command +sub cmd { + my $cmd = shift; + my $prompt = shift; + $prompt = "Command: $cmd.." unless ($prompt); + print $prompt; + system($cmd); + my $exit_value = $? >> 8; + #my $signal_num = $? & 127; + #my $dumped_core = $? & 128; + if ($exit_value == 0) { + print "ok\n"; + } else { + ++$errCount; + print "ERROR ($exit_value)\n"; + exit(1); + } +} + +#----------------------------------------------------------------------- +sub usage { + print << "END"; +Usage: +xml2res.pl +Options: + --lib= + --sourcedir= + --icuroot= + --xerces= + --xml2txt= + --genrb= + +xml2res creates *.res file from *.xml files by invoking the respective tools +Optionally, one or more locales may be specified on the command line. +If this is done, only those locales will be processed. If no locales +are listed, all locales are processed. + +END + exit(0); +} \ No newline at end of file diff --git a/icu4c/source/samples/xml2txt/xml2txt.cpp b/icu4c/source/samples/xml2txt/xml2txt.cpp new file mode 100644 index 00000000000..1eb35ff0e9f --- /dev/null +++ b/icu4c/source/samples/xml2txt/xml2txt.cpp @@ -0,0 +1,899 @@ +#include "xml2txt.h" + +static bool DTDFLAG = false; +static char* gTxtFile; +static char* gXmlFile; +static const char *sourceDir; +static const char *destDir; +static bool gDoNamespaces = false; +static bool gDoSchema = false; +static bool gDoCreate = false; +static XMLCh* gEncodingName = 0; +static XMLFormatter::UnRepFlags gUnRepFlags = XMLFormatter::UnRep_CharRef; +static DOMParser::ValSchemes gValScheme = DOMParser::Val_Auto; +static XMLFormatter* gFormatter = 0; + + + +enum +{ + HELP, + SOURCEDIR, + DESTDIR, +}; +//#define UOPTION_TXT UOPTION_DEF("txt", 't', UOPT_NO_ARG) +//#define UOPTION_RES UOPTION_DEF("res", 'r', UOPT_NO_ARG) + +UOption options[]={ + UOPTION_HELP_H, + UOPTION_SOURCEDIR, + UOPTION_DESTDIR, + }; + + + +#ifdef XP_MAC_CONSOLE +#include +#endif + + +// --------------------------------------------------------------------------- +// +// Usage() +// +// --------------------------------------------------------------------------- +void usage() +{ + cout << "\nUsage: XML2TXT [OPTIONS] [FILES]\n\n" + "This program is used to convert XML files to TXT files.\n" + "Please refer to the following options. Options are not \n" + "case sensitive.\n" + "Options:\n" + "\t-s or --sourcedir \t source directory for files followed by path, default is current directory.\n" + "\t-d or --destdir \t destination directory, followed by the path, default is current directory.\n" + "\t-h or -? or --help \t this usage text.\n" + "\nAttention: \n" + "\tThe text file's encoding is the same as the source file's.\n" + + << endl; +} + +int main(int argC, char* argV[]) +{ + int retval = 0; + const char* arg=NULL; + + try + { + XMLPlatformUtils::Initialize(); + } + + catch(const XMLException& toCatch) + { + cerr << "Error during Xerces-c Initialization.\n" + << " Exception message:" + << DOMString(toCatch.getMessage()) << endl; + return 1; + } + + #ifdef XP_MAC_CONSOLE + + argC = ccommand((char***)&argV); + #endif + + argC = u_parseArgs(argC, argV, (int32_t)(sizeof(options)/sizeof(options[0])), options); + + if(argC<0) { + cout << "error in command line argument" << argV[-argC] << endl; + } + + // Watch for special case help request + if(argC<2 || options[HELP].doesOccur) { + usage(); + return argC < 0 ? U_ILLEGAL_ARGUMENT_ERROR : U_ZERO_ERROR; + } + + if(options[SOURCEDIR].doesOccur) { + sourceDir = options[SOURCEDIR].value; + } + else { + #ifdef WIN32 + destDir = _getcwd(NULL, 0); + #else + destDir = getcwd(NULL, 0); + #endif + } + + if(options[DESTDIR].doesOccur) { + destDir = options[DESTDIR].value; + } + else { + #ifdef WIN32 + destDir = _getcwd(NULL, 0); + #else + destDir = getcwd(NULL, 0); + #endif + } + + for(int i = 1; i< argC; i++) { + arg = getLongPathname(argV[i]); + + gXmlFile = CreateFile(arg, sourceDir); + + gTxtFile = CreateTxtName(arg, destDir); + + + retval = ProcessTxtFile(); + } + XMLPlatformUtils::Terminate(); + return retval; +} + + + +int ProcessTxtFile() +{ + int retval = 0; + + DOMParser* parser; + DOMTreeErrorReporter* errReporter; + parser = new DOMParser(); + errReporter = new DOMTreeErrorReporter(); + parser->setValidationScheme(gValScheme); + parser->setDoNamespaces(true); + parser->setDoSchema(gDoSchema); + + parser->setErrorHandler(errReporter); + parser->setCreateEntityReferenceNodes(gDoCreate); + parser->setToCreateXMLDeclTypeNode(true); + + // + // Parse the XML file, catching any XML exceptions that might propogate + // out of it. + // + bool errorsOccured = false; + try + { + parser->parse(gXmlFile); + int errorCount = parser->getErrorCount(); + if (errorCount > 0) + errorsOccured = true; + } + + catch (const XMLException& e) + { + + + cerr << "An error occured during parsing\n Message: " + << DOMString(e.getMessage()) << endl; + errorsOccured = true; + } + + + catch (const DOM_DOMException& e) + { + cerr << "A DOM error occured during parsing\n DOMException code: " + << e.code << endl; + errorsOccured = true; + } + + catch (...) + { + cerr << "An error occured during parsing\n " << endl; + errorsOccured = true; + } + + if(!errorsOccured && !errReporter->getSawErrors()) + { + DOM_Node document = parser->getDocument(); + Check(document); //if check fails, exit(0); else excute the following code + if(DTDFLAG == false){ + cout << "DTD no assigned!" << endl; + exit(0); + } + } + + // If the parse and doubt-check was successful, output the document data from the DOM tree + if (!errorsOccured && !errReporter->getSawErrors()) + { + DOM_Node doc = parser->getDocument(); + DOMPrintFormatTarget *formatTarget = new DOMPrintFormatTarget(gTxtFile); + + + + if (gEncodingName == 0) + { + DOMString encNameStr("UTF-8"); + DOM_Node aNode = doc.getFirstChild(); + if (aNode.getNodeType() == DOM_Node::XML_DECL_NODE) + { + DOMString aStr = ((DOM_XMLDecl &)aNode).getEncoding(); + if (aStr != "") + { + encNameStr = aStr; + } + } + unsigned int lent = encNameStr.length(); + gEncodingName = new XMLCh[lent + 1]; + XMLString::copyNString(gEncodingName, encNameStr.rawBuffer(), lent); + gEncodingName[lent] = 0; + } + + + try + { + gFormatter = new XMLFormatter(gEncodingName, formatTarget, + XMLFormatter::NoEscapes, gUnRepFlags); + ofstream ofile(gTxtFile, ios::trunc); + cout << doc; + } + catch (XMLException& e) + { + cerr << "An error occurred during creation of output transcoder. Msg is:" + << endl + << DOMString(e.getMessage()) << endl; + retval = 3; + } + delete formatTarget; + delete gFormatter; + } + delete errReporter; + delete parser; + parser = NULL; + errReporter = NULL; + delete gEncodingName; + gEncodingName=NULL; + return retval; +} + + + + + + +//---------------------------------------------------------------------------- +// double-check before DOM Tree PrintOut +//---------------------------------------------------------------------------- +void Check( DOM_Node &document) +{ + // Get the name and value out for convenience + DOMString nodeName = document.getNodeName(); //, type + DOMString nodeValue = document.getNodeValue(); // + + DOMString attributeKey, attributeVal; //(key/name)(val/filename) + unsigned long lent = nodeValue.length(); + switch (document.getNodeType()) + { + case DOM_Node::TEXT_NODE: + { + break; + } + + case DOM_Node::PROCESSING_INSTRUCTION_NODE : + { + break; + } + + + case DOM_Node::DOCUMENT_NODE : + { + + DOM_Node child = document.getFirstChild(); + while( child != 0) + { + Check(child); + child = child.getNextSibling(); + } + break; + } + + case DOM_Node::ELEMENT_NODE : + { + DOM_NamedNodeMap attributes = document.getAttributes(); + int attrCount = attributes.getLength(); + int item_num=0; + for (int i = 0; i < attrCount; i++) + { + DOM_Node attribute = attributes.item(i); + + if(attribute.getNodeName().equals("key")||attribute.getNodeName().equals("name")){ + attributeKey = attribute.getNodeValue(); + } + else if(attribute.getNodeName().equals("val")||attribute.getNodeName().equals("filename")){ + attributeVal = attribute.getNodeValue(); + item_num = i; + } + else{ + //call error report + ErrorReport(document, 0); + } + } + + if(document.getParentNode().getNodeName().equals("array") && attributeKey!=NULL){ + ErrorReport(document, 1); //ErrorType =1--the element in the array has name + } + else if(document.getParentNode().getNodeName().equals("table") && attributeKey==NULL){ + ErrorReport(document, 2); //element in a table has no name + } + + if(document.getNodeName().equals("table")) + { + //unsigned int Child_Num; + if(document.hasChildNodes()) + { + ChildName* cn = new ChildName(); + cn->SetNext(NULL); + ChildName* head = CheckNameDuplicate(document, cn); + DelChildName(head); + } + } + else if(document.getNodeName().equals("array")) {} + else if(document.getNodeName().equals("resourceBundle")) {} + + else if(document.getNodeName().equals("str")||document.getNodeName().equals("importBin")) + { + CheckEscape(attributes, attributeVal, item_num); + } + + else if(document.getNodeName().equals("intVector")) + { + DOMString ivstring; + ivstring = CheckIntvector(attributeVal, document); + if(ivstring !=NULL) + attributes.item(item_num).setNodeValue(ivstring); + } + + else if(document.getNodeName().equals("int")) + { + CheckInt(attributeVal, document); + } + + else if(document.getNodeName().equals("bin")) + { + CheckBin(attributeVal, document); + } + + else if(document.getNodeName().equals("import")) {} + else if(document.getNodeName().equals("alias")) {} + else { + ErrorReport(document, 6); + } + + + DOM_Node child = document.getFirstChild(); + if (child != 0) + { + while( child != 0) + { + Check(child); + child = child.getNextSibling(); + } + } + break; + } + + + case DOM_Node::ENTITY_REFERENCE_NODE: + { + break; + } + + + case DOM_Node::CDATA_SECTION_NODE: + { + break; + } + + + case DOM_Node::COMMENT_NODE: + { + break; + } + + + case DOM_Node::DOCUMENT_TYPE_NODE: + { + DTDFLAG = true; + break; + } + + + case DOM_Node::ENTITY_NODE: + { + break; + } + + + case DOM_Node::XML_DECL_NODE: + { + break; + } + + + default: + cerr << "Unrecognized node type = " + << (long)document.getNodeType() << endl; + } +} + +void CheckEscape(DOM_NamedNodeMap attributes, DOMString attributeVal, int item_num) +{ + unsigned int len; + char Escape[7] = {'\\', 'u', '0', '0', '2', '2', '\0'}; + len = attributeVal.length(); + DOMString fromStr; + DOMString toStr; + const XMLCh quote[] = {(unsigned short)0x22, (unsigned short) 0}; + if(len>0) + { + for(unsigned int i=0; iNext; + while(temp!=NULL) + { + delete cn; + cn = NULL; + cn = temp; + temp = temp->Next; + } + delete cn; +} + +ChildName* CheckNameDuplicate(DOM_Node document, ChildName* cn) +{ + DOM_Node CNode = document.getFirstChild(); + + while(CNode!=NULL) + { + if(CNode.getNodeName().equals("string")||CNode.getNodeName().equals("bin")||CNode.getNodeName().equals("int")||CNode.getNodeName().equals("intvector")||CNode.getNodeName().equals("import")||CNode.getNodeName().equals("table")||CNode.getNodeName().equals("array")) + { + DOMString cname = getAttributeKey(CNode); + char* string = cname.transcode(); + ChildName* temp = cn; + while(temp->Next!=NULL) + { + if(cname.equals(temp->Name)) + { + DelChildName(cn); + ErrorReport(CNode, 5); //name duplication + } + temp = temp ->Next; + } + + ChildName* childname = new ChildName(); + childname->SetName(cname); + childname->SetNext(cn); + cn = childname; + } + CNode = CNode.getNextSibling(); + } + return cn; +} + +unsigned int GetCNodeNum(DOM_Node document) +{ + unsigned int num=0; + DOM_Node CNode = document.getFirstChild(); + while(CNode!=NULL) + { + if(CNode.getNodeName().equals("string")||CNode.getNodeName().equals("bin")||CNode.getNodeName().equals("int")||CNode.getNodeName().equals("intvector")||CNode.getNodeName().equals("import")||CNode.getNodeName().equals("table")||CNode.getNodeName().equals("array")) + num++; + CNode = CNode.getNextSibling(); + } + return num; +} + +void CheckBin(DOMString attributeVal, DOM_Node document) +{ + char *stopstring; + char toConv[2] = {'\0', '\0'}; + char* string = attributeVal.transcode(); + int count = strlen(string); + if(count > 0) + { + if((count % 2)==0) + { + for(int i=0; i, type + DOMString nodeValue = toWrite.getNodeValue(); // + + DOMString attributeKey, attributeVal; //(key/name)(val/filename) + unsigned long lent = nodeValue.length(); + + + switch (toWrite.getNodeType()) + { + case DOM_Node::TEXT_NODE: + { + gFormatter->formatBuf(nodeValue.rawBuffer(), + lent, XMLFormatter::CharEscapes); + break; + } + + + case DOM_Node::PROCESSING_INSTRUCTION_NODE : + { + break; + } + + + case DOM_Node::DOCUMENT_NODE : + { + + DOM_Node child = toWrite.getFirstChild(); + while( child != 0) + { + target << child; + child = child.getNextSibling(); + } + break; + } + + + case DOM_Node::ELEMENT_NODE : + { + + DOM_NamedNodeMap attributes = toWrite.getAttributes(); + int attrCount = attributes.getLength(); + for (int i = 0; i < attrCount; i++) + { + DOM_Node attribute = attributes.item(i); + + if(attribute.getNodeName().equals("key")||attribute.getNodeName().equals("name")){ + attributeKey = attribute.getNodeValue(); + } + else if(attribute.getNodeName().equals("val")||attribute.getNodeName().equals("filename")){ + attributeVal = attribute.getNodeValue(); + } + } + + //Print Out + if(nodeName.equals("resourceBundle")) + *gFormatter << attributeKey; + else + { + if(nodeName.equals("bin") && attributeVal==NULL) + *gFormatter <=0; i--) + { + attribute = attributes.item(i); + ErrorMsg.insertData(0, " ; "); + ErrorMsg.insertData(0, attribute.getNodeValue()); + } + } + ErrorMsg.insertData(0, "("); + ErrorMsg.insertData(0, toWrite.getNodeName()); + ErrorMsg.insertData(0, "==>"); + toWrite = toWrite.getParentNode(); + } + ErrorMsg.appendData("\n"); + + switch (ErrorType) + { + case 1: + ErrorMsg.appendData("The element in the array can't have a name!\n"); + break; + case 2: + ErrorMsg.appendData("The element in the table should have a name!\n"); + break; + case 3: + ErrorMsg.appendData("Invalid integer value!\n"); + break; + case 4: + ErrorMsg.appendData("Invalid bin!\n"); + break; + case 5: + ErrorMsg.appendData("Name Duplication in the table!\n"); + break; + case 6: + ErrorMsg.appendData("Invalid element name! Remember to assign correct DTD file on the xml file.\n"); + break; + } + cout << ErrorMsg; + exit(0); +} + +char* CreateTxtName(const char* arg, const char* Dir) +{ + char* temp = CreateFile(arg, Dir); + int len = strlen(temp); + temp[len-1] = 't'; + temp[len-2] = 'x'; + temp[len-3] = 't'; + return temp; + + /*char drive[_MAX_DRIVE]; + char dir[_MAX_DIR]; + char fname[_MAX_FNAME]; + char ext[_MAX_EXT]; + _splitpath(gXmlFile, drive, dir, fname, ext); + strcpy(gTxtFile, "\0"); + if (drive != NULL) { + strcat(gTxtFile, drive); + } + if (dir != NULL) { + strcat(gTxtFile, dir); + } + if (fname !=NULL) { + strcat(gTxtFile, fname); + } + strcat(gTxtFile, "tempfile.txt");*/ +} + +char* CreateFile(const char* arg, const char* Dir) +{ char* temp = new char[256]; + char a[2]={'\\', '\0'}; + char* currdir; + if(sourceDir!=NULL) { + strcpy(temp, Dir); + int len = strlen(temp); + if(temp[len - 1]!='\\') + strcat(temp, a); + strcat(temp, arg); + } + else { + char drive[_MAX_DRIVE]; + char dir[_MAX_DIR]; + char fname[_MAX_FNAME]; + char ext[_MAX_EXT]; + _splitpath(arg, drive, dir, fname, ext); + + if(*drive == NULL && *dir == NULL) { + #ifdef WIN32 + currdir = _getcwd(NULL, 0); + #else + currdir = getcwd(NULL, 0); + #endif + strcpy(temp, currdir); + strcat(temp, a); + } + strcat(temp, arg); + } + return temp; +} + + +// --------------------------------------------------------------------------- +// ostream << DOMString +// +// Stream out a DOM string. Doing this requires that we first transcode +// to char * form in the default code page for the system +// --------------------------------------------------------------------------- + +ostream& operator<< (ostream& target, const DOMString& s) +{ + char *p = s.transcode(); + target << p; + delete [] p; + return target; +} + + +XMLFormatter& operator<< (XMLFormatter& strm, const DOMString& s) +{ + unsigned int lent = s.length(); + + if (lent <= 0) + return strm; + + XMLCh* buf = new XMLCh[lent + 1]; + XMLString::copyNString(buf, s.rawBuffer(), lent); + buf[lent] = 0; + strm << buf; + delete [] buf; + return strm; +} + + diff --git a/icu4c/source/samples/xml2txt/xml2txt.dsp b/icu4c/source/samples/xml2txt/xml2txt.dsp new file mode 100644 index 00000000000..4174bb505d6 --- /dev/null +++ b/icu4c/source/samples/xml2txt/xml2txt.dsp @@ -0,0 +1,143 @@ +# Microsoft Developer Studio Project File - Name="xml2txt" - Package Owner=<4> +# Microsoft Developer Studio Generated Build File, Format Version 6.00 +# ** DO NOT EDIT ** + +# TARGTYPE "Win32 (x86) Console Application" 0x0103 + +CFG=xml2txt - Win32 Debug +!MESSAGE This is not a valid makefile. To build this project using NMAKE, +!MESSAGE use the Export Makefile command and run +!MESSAGE +!MESSAGE NMAKE /f "xml2txt.mak". +!MESSAGE +!MESSAGE You can specify a configuration when running NMAKE +!MESSAGE by defining the macro CFG on the command line. For example: +!MESSAGE +!MESSAGE NMAKE /f "xml2txt.mak" CFG="xml2txt - Win32 Debug" +!MESSAGE +!MESSAGE Possible choices for configuration are: +!MESSAGE +!MESSAGE "xml2txt - Win32 Release" (based on "Win32 (x86) Console Application") +!MESSAGE "xml2txt - Win32 Debug" (based on "Win32 (x86) Console Application") +!MESSAGE + +# Begin Project +# PROP AllowPerConfigDependencies 0 +# PROP Scc_ProjName "" +# PROP Scc_LocalPath "" +CPP=cl.exe +RSC=rc.exe + +!IF "$(CFG)" == "xml2txt - Win32 Release" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 0 +# PROP BASE Output_Dir "Release" +# PROP BASE Intermediate_Dir "Release" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 0 +# PROP Output_Dir "Release" +# PROP Intermediate_Dir "Release" +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /c +# ADD CPP /nologo /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /c +# ADD BASE RSC /l 0x409 /d "NDEBUG" +# ADD RSC /l 0x409 /d "NDEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 +# ADD LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /machine:I386 + +!ELSEIF "$(CFG)" == "xml2txt - Win32 Debug" + +# PROP BASE Use_MFC 0 +# PROP BASE Use_Debug_Libraries 1 +# PROP BASE Output_Dir "Debug" +# PROP BASE Intermediate_Dir "Debug" +# PROP BASE Target_Dir "" +# PROP Use_MFC 0 +# PROP Use_Debug_Libraries 1 +# PROP Output_Dir "Debug" +# PROP Intermediate_Dir "Debug" +# PROP Ignore_Export_Lib 0 +# PROP Target_Dir "" +# ADD BASE CPP /nologo /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /Yu"stdafx.h" /FD /GZ /c +# ADD CPP /nologo /MDd /W3 /Gm /GX /ZI /Od /I "..\..\..\include" /I "..\..\tools\toolutil" /I "C:\work\xerces-c\xerces-c-src1_6_0\src" /D "WIN32" /D "_DEBUG" /D "_CONSOLE" /D "_MBCS" /FD /GZ /c +# SUBTRACT CPP /YX /Yc /Yu +# ADD BASE RSC /l 0x409 /d "_DEBUG" +# ADD RSC /l 0x409 /d "_DEBUG" +BSC32=bscmake.exe +# ADD BASE BSC32 /nologo +# ADD BSC32 /nologo +LINK32=link.exe +# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept +# ADD LINK32 icuind.lib icuucd.lib icutud.lib xerces-c_1D.lib /nologo /subsystem:console /debug /machine:I386 /pdbtype:sept /libpath:"..\..\..\lib" /libpath:"C:\work\xerces-c\xerces-c-src1_6_0\Build\Win32\VC6\Debug" +# Begin Custom Build +InputPath=.\Debug\xml2txt.exe +InputName=xml2txt +SOURCE="$(InputPath)" + +"..\..\..\..\bin\$(InputName).exe" : $(SOURCE) "$(INTDIR)" "$(OUTDIR)" + copy $(InputPath) ..\..\..\..\bin + +# End Custom Build + +!ENDIF + +# Begin Target + +# Name "xml2txt - Win32 Release" +# Name "xml2txt - Win32 Debug" +# Begin Group "Source Files" + +# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat" +# Begin Source File + +SOURCE=.\ChildName.cpp +# End Source File +# Begin Source File + +SOURCE=.\DOMPrintFormatTarget.cpp +# End Source File +# Begin Source File + +SOURCE=.\DOMTreeErrorReporter.cpp +# End Source File +# Begin Source File + +SOURCE=.\xml2txt.cpp +# End Source File +# End Group +# Begin Group "Header Files" + +# PROP Default_Filter "h;hpp;hxx;hm;inl" +# Begin Source File + +SOURCE=.\ChildName.h +# End Source File +# Begin Source File + +SOURCE=.\DOMPrintFormatTarget.h +# End Source File +# Begin Source File + +SOURCE=.\DOMTreeErrorReporter.hpp +# End Source File +# Begin Source File + +SOURCE=.\xml2txt.h +# End Source File +# End Group +# Begin Group "Resource Files" + +# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe" +# End Group +# Begin Source File + +SOURCE=.\ReadMe.txt +# End Source File +# End Target +# End Project diff --git a/icu4c/source/samples/xml2txt/xml2txt.h b/icu4c/source/samples/xml2txt/xml2txt.h new file mode 100644 index 00000000000..d1553c9daf5 --- /dev/null +++ b/icu4c/source/samples/xml2txt/xml2txt.h @@ -0,0 +1,45 @@ +#ifndef xml2txt_H_ +#define xml2txt_H_ + +#include +#include +#include +#ifdef WIN32 +#include +#else +#include +#endif + +#include +#include + + +#include "uoptions.h" +#include "toolutil.h" +#include "ChildName.h" +#include "DOMPrintFormatTarget.h" + +//#define UOPTION_TXT UOPTION_DEF("txt", 't', UOPT_NO_ARG) +//#define UOPTION_RES UOPTION_DEF("res", 'r', UOPT_NO_ARG) + +void usage(); +void InitParser(); +void recycle(); +int ProcessTxtFile(); +void ErrorReport(DOM_Node& towrite, int ErrorType); +void Check(DOM_Node &document); +ostream& operator<<(ostream& target, const DOMString& toWrite); +ostream& operator<<(ostream& target, DOM_Node& toWrite); +XMLFormatter& operator<< (XMLFormatter& strm, const DOMString& s); +char* CreateTxtName(const char* arg, const char* Dir); +char* CreateFile(const char* arg, const char* Dir); +DOMString CheckIntvector(DOMString attributeVal, DOM_Node document); +void CheckInt(DOMString attributeVal, DOM_Node document); +void CheckBin(DOMString attributeVal, DOM_Node document); +unsigned int GetCNodeNum(DOM_Node document); +ChildName* CheckNameDuplicate(DOM_Node document, ChildName* cn); +DOMString getAttributeKey(DOM_Node CNode); +void DelChildName(ChildName* cn); +void CheckEscape(DOM_NamedNodeMap attributes, DOMString attributeVal, int item_num); + +#endif \ No newline at end of file diff --git a/icu4c/source/samples/xml2txt/xml2txt.plg b/icu4c/source/samples/xml2txt/xml2txt.plg new file mode 100644 index 00000000000..1b3e8493044 --- /dev/null +++ b/icu4c/source/samples/xml2txt/xml2txt.plg @@ -0,0 +1,16 @@ + + +
+

Build Log

+

+--------------------Configuration: xml2txt - Win32 Debug-------------------- +

+

Command Lines

+ + + +

Results

+xml2txt.exe - 0 error(s), 0 warning(s) +
+ +