int main()

in xsec/tools/c14n/c14n.cpp [70:215]


int main(int argc, char **argv) {

    const char* id = NULL;
	bool printComments = true;
    bool exclusive = false;
    bool inclusive11 = false;

	// Check arguments

	if (argc < 2) {
		printUsage();
		exit (1);
	}

	if (argc > 2) {
		for (int i = 1; i < argc - 1; ++i) {

			if (!strcmp(argv[i], "-n") || !strcmp(argv[i], "-N"))
				printComments = false;
            else if (!strcmp(argv[i], "-x") || !strcmp(argv[i], "-X"))
                exclusive = true;
            else if (!strcmp(argv[i], "-1.1"))
                inclusive11 = true;
            else if (!strcmp(argv[i], "-id") && argc > i + 1)
                id = argv[++i];
			else {
				cerr << "Unknown option %s\n\n";
				printUsage();
				exit (1);
			}
		}
	}

				

	// Initialise the XML system

	try {

		XMLPlatformUtils::Initialize();
		XSECPlatformUtils::Initialise();

	}
	catch (const XMLException &e) {

		cerr << "Error during initialisation of Xerces" << endl;
		cerr << "Error Message = : "
		     << e.getMessage() << endl;

	}

	// Create and set up the parser

	XercesDOMParser * parser = new XercesDOMParser;
	parser->setDoNamespaces(true);
    parser->setValidationScheme(XercesDOMParser::Val_Never);
	parser->setDoSchema(false);
	parser->setCreateEntityReferenceNodes(false);

	// Now parse out file

	bool errorsOccured = false;
	XMLSize_t errorCount = 0;
    try
    {
    	parser->parse(argv[argc-1]);
        errorCount = parser->getErrorCount();
        if (errorCount > 0)
            errorsOccured = true;
    }

    catch (const XMLException& e)
    {
        cerr << "An error occurred during parsing\n   Message: "
             << e.getMessage() << endl;
        errorsOccured = true;
    }


    catch (const DOMException& e)
    {
       cerr << "A DOM error occurred during parsing\n   DOMException code: "
             << e.code << endl;
        errorsOccured = true;
    }

	if (errorsOccured) {

		cout << "Errors during parse" << endl;
		exit (1);

	}

	/*

		Now that we have the parsed file, get the DOM document and start looking at it

	*/
	
	DOMNode *subtree = NULL;
	DOMDocument *theDOM = parser->getDocument();
    if (id) {
        XMLCh* temp = XMLString::transcode(id);
        subtree = theDOM->getElementById(temp);
        XSEC_RELEASE_XMLCH(temp);
        if (!subtree) {
            cerr << "ID reference did not resolve" << endl;
            exit(1);
        }
    }

	// Create the canonicalizer
	XSECC14n20010315* canon=NULL;
	if (subtree)
		canon = new XSECC14n20010315(theDOM, subtree);
	else
		canon = new XSECC14n20010315(theDOM);
	canon->setCommentsProcessing(printComments);
	canon->setUseNamespaceStack(true);
	if (inclusive11)
		canon->setInclusive11();
	else if (exclusive)
		canon->setExclusive();

	// canon->XPathSelectNodes("(/descendant-or-self::node() | /descendant-or-self::node()/attribute::* | /descendant-or-self::node()/namespace::*)[ self::ietf:e1 or (parent::ietf:e1 and not(self::text() or self::e2)) or count (id(\"E3\") | ancestor-or-self::node()) = count (ancestor-or-self::node())]");

	char buffer[512];
	XMLSize_t res = canon->outputBuffer((unsigned char *) buffer, 128);


	while (res != 0) {
		buffer[res] = '\0';
		cout << buffer;
		res = canon->outputBuffer((unsigned char *) buffer, 128);
	}

	cout << endl;

    delete canon;
    delete parser;

    XSECPlatformUtils::Terminate();
	XMLPlatformUtils::Terminate();

	return 0;
}