package gov.nist.hitsp.validation; import java.io.BufferedReader; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import net.ihe.gazelle.tools.comparator.DocumentValidator; import net.ihe.gazelle.tools.comparator.FileReadWrite; import net.ihe.gazelle.validation.Error; import net.ihe.gazelle.validation.Notification; public class NistBasicValidation implements DocumentValidator { private static String pathres = "/home/aboufahj/Documents/ihic/tools-res/nistres/"; public static void main(String[] args) { new NistBasicValidation().validateDocument( "/home/aboufahj/workspaceTopcased/cdabasic-validator-jar/src/test/resources/samples/ActSpec_rmim001_1_OK_1.xml"); } public List validateDocument(String filepath) { File fil = new File(filepath); File resFile = new File(pathres + fil.getName().split("\\.")[0] + ".txt"); String res = validate(filepath, resFile); List listNot = transformResultIntoListNotif(res); System.out.println(fil.getName()); System.out.println(res); return listNot; } private List transformResultIntoListNotif(String res) { List listNot = new ArrayList(); String regex = "Severity:(.*?)\nSpecification:(.*?)\nMessage:(.*?)\nContext:(.*?)\nTest:(.*?)\n"; Pattern pat = Pattern.compile(regex, Pattern.DOTALL|Pattern.MULTILINE); Matcher mat = pat.matcher(res); while (mat.find()){ if (mat.group(1).contains("error")){ Notification not = new Error(); not.setDescription(mat.group(3)); not.setLocation(mat.group(4)); listNot.add(not); } } return listNot; } private String validate(String filepath, File resFile){ if (resFile.exists()) { try{ return FileReadWrite.readDoc(resFile.getAbsolutePath()); } catch(Exception e){ e.printStackTrace(); return ""; } } StringBuffer result = new StringBuffer(); String validatorpath = "java -jar /home/aboufahj/Documents/ihic/tools/cdaValidateClient-nist/hitspValidationWSClient.jar -validate " + " -specification cdar2 -wsUrl http://cda-validation.nist.gov:11080/ws/services/ValidationWebService?wsdl " + "-document "; try { Process p = Runtime.getRuntime().exec(validatorpath + " " + filepath); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getInputStream())); String s = new String(); while((s = stdError.readLine()) != null){ result.append(s + "\n"); } } catch (IOException e) { result = new StringBuffer(); result.append("A problem occurred when validating : " + e.getMessage()); e.printStackTrace(); } String maChaine = result.toString(); try{ FileOutputStream fos = new FileOutputStream(resFile); fos.write(maChaine.getBytes()); fos.close(); } catch(Exception e){ e.printStackTrace(); } return maChaine; } }