Package org.sintef.umt.transgen

Source Code of org.sintef.umt.transgen.Validator

// UML Model Transformation Tool (UMT)
// Copyright (C) 2003, 2004, 2005 SINTEF
// Authors:  jon.oldevik at sintef.no | roy.gronmo at sintef.no | tor.neple at sintef.no | fredrik.vraalsen at sintef.no
// Webpage: http://umt.sourceforge.net
// Deloped in the projects:  ACEGIS (EU project - IST-2002-37724),
//    CAFE (EUREKA/ITEA - ip00004), FAMILIES (ITEA project ip02009)
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation; either version 2.1
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
// 02111-1307 USA

package org.sintef.umt.transgen;


import javax.xml.parsers.*;
import org.xml.sax.*;
import org.sintef.umt.transformer.XMLUtility;
import org.sintef.umt.umtmain.MyURIResolver;
import org.sintef.umt.umtmain.OutputListener;
import org.sintef.umt.umtmain.UMTMain;
import org.w3c.dom.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;

import java.io.*;

/**
/**
* @author Tor Neple, tne@sintef.no
* copyright (c) 2002, SINTEF
*
* Utility class to validate XML documents agains XML schemas
* to be used to validate XMI files against schema for the right
* UMT profile.
*/
public class Validator {
  static final boolean CASE_SENSITIVE = true
    static final String JAXP_SCHEMA_LANGUAGE =
        "http://java.sun.com/xml/jaxp/properties/schemaLanguage";
    static final String W3C_XML_SCHEMA =
        "http://www.w3.org/2001/XMLSchema";
    static final String JAXP_SCHEMA_SOURCE =
        "http://java.sun.com/xml/jaxp/properties/schemaSource";
       
    static final String outputEncoding = "UTF-8";
   
    private OutputListener theListener;
   
    public Validator(OutputListener listener){
      theListener = listener; 
    }
 
  public void validateAgainstSchema(String XMLFileName, String XMLSchemaFileName) throws Exception{
    // Need to make the xml-schema case-insensitive.
    // for this we use a nifty little xslt transformation from IBM
    if (CASE_SENSITIVE){
      try
        XMLUtility xmlutil = new XMLUtility();
        // xmlutil.addLookupPath(UMTMain.resource_dir);
        File schemaFile = new File(XMLSchemaFileName);
        File convertFile = new File(UMTMain.resource_dir + "convert-enumerations.xsl");
        String insensitiveSchema = xmlutil.transform(schemaFile,convertFile);
        FileWriter writer = new FileWriter (new File("insensitive_"+XMLSchemaFileName))
        writer.write(insensitiveSchema);
        writer.close();
        writer = null;   
      } catch (Exception ex) {
        ex.printStackTrace();
      }
    }
   
   
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);
    dbf.setValidating(true);
    try{
    }
    catch(IllegalArgumentException x){
      System.out.println("Error : Jaxp DocumentBuilderFactory property not supported");
      System.out.println("Check if the parser is JAXP 1.2 conformant");
      System.exit(1);
    }
    if (CASE_SENSITIVE)
      dbf.setAttribute(JAXP_SCHEMA_SOURCE, new File("insensitive_"+XMLSchemaFileName));
    else 
      dbf.setAttribute(JAXP_SCHEMA_SOURCE, new File(XMLSchemaFileName));
     
    dbf.setAttribute(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);   
    DocumentBuilder db = dbf.newDocumentBuilder();
    MyURIResolver resolver = new MyURIResolver();
    resolver.addLookup(UMTMain.resource_dir);
    db.setEntityResolver(resolver);
    OutputStreamWriter errorWriter = new OutputStreamWriter(System.err, outputEncoding);
    ErrorHandler errHand = new org.sintef.umt.umtmain.ShortErrorListener(theListener);
    db.setErrorHandler(errHand);
    Document doc = db.parse(new File(XMLFileName));

  }
 
  public void validateAgainstSchema(Document model, XmiSchema schema) throws Exception{
    // This is the dirty way.  Make a schemafile..
    schema.storeSchema("temp_schema.xsd");
    try {
   
/*      factory = DocumentBuilderFactory.newInstance ();
      parser = factory.newDocumentBuilder();
      doc = parser.newDocument();
      doc.appendChild(theSchema);*/
      TransformerFactory tfactory = TransformerFactory.newInstance();
      Transformer transformer = tfactory.newTransformer();
      FileWriter writer = new FileWriter ("temp_model.xml");       
      transformer.transform(new javax.xml.transform.dom.DOMSource(model), new StreamResult(writer));
      writer.close();
      writer = null;   
    } catch (Exception ex) {
      ex.printStackTrace();
      System.out.println ("PropertyManager::Validation -->" + ex);
    }
   
    validateAgainstSchema("temp_model.xml","temp_schema.xsd")
  }
 
 


}
TOP

Related Classes of org.sintef.umt.transgen.Validator

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.