Calling JCam Example

When writing the CAM Editor - CAMed - It was found that a short cut way of calling the JCam processor would be helpful.

The following is an example of what is required.

/**
 * Copyright British Telecommunications plc - 2008

 */

package uk.org.jcam.example;

import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.jaxen.JaxenException;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.JDOMException;

import uk.org.jcam.CAM;
import uk.org.jcam.CAMException;
import uk.org.jcam.JCam;
import uk.org.jcam.processor.dataObjects.Parameters.ParametersException;
import uk.org.jcam.reader.DocumentFactory;
import uk.org.jcam.util.Cam;
import uk.org.jcam.xpath.Xpath;

/**
 * @author Martin Roberts
 *
 */
public class CallingJCam {
        


        /**
         * @param args
         */
        public static void main(String[] args) {
                JCam.flagStates.setVerbose(true);
                //JCam.flagStates.setTrace(true);
                String templateFileName = args[0];
                String xmlDocumentFilename = args[1];
                String structureID = args[2];
                DocumentFactory df = new DocumentFactory();
                Document templateDocument;
                try {
                        templateDocument = df.createDocument(templateFileName);

                        Document xmlDocument = df.createDocument(xmlDocumentFilename);

                        String[] parameters = {};

                        JCam processor = new JCam(JCam.Options.VALIDATE,templateDocument,templateFileName,xmlDocument,parameters);
                        Document res = null;
                        res = processor.run(structureID);
                        
                        //Either use the processor to get errors 
                        if (processor.hasErrors()){
                                String[] errors = processor.getErrors();
                                if (errors != null) {//if exception occured in fetching errors null is returned
                                        System.err.println("Errors found: ");
                                        for (int i= 1;i<errors.length;i++){
                                                System.err.println(errors[i]);
                                        }
                                }
                        }
                        
                        //or use the routine below to xpath into the errors
                        printErrors(res);
                } catch (JDOMException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (CAMException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (JaxenException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }       
        }
        
        private static void printErrors(Document xmlfile) throws ParametersException, JaxenException, JDOMException  {
                if (CAM.trace) 
                        Cam.traceOutput();
                Xpath xpath = new Xpath();
                        xpath.setUpXPath(xmlfile,"//*/@"+Cam.ATTR_CAMERROR);
                        List errors = xpath.getXPath().selectNodes(xmlfile);
                        if (errors.isEmpty()) {
                                Iterator eI = errors.iterator();
                                while (eI.hasNext()) {
                                        Attribute a = (Attribute) eI.next();
                                        String[] msgs = a.getValue().split("%");
                                        for (int i = 0; i < msgs.length; i++){
                                                if (!(msgs[i].startsWith("Error")))
                                                        System.out.print("\t");
                                                System.out.println(msgs[i]);
                                        }
                                }
                        } else {
                                System.out.println("No Errors");;
                        }                       
                        
        }

}