View Javadoc

1   package org.dbe.studio.tools.ssl2sdl.util;
2   
3   import java.io.*;
4   import org.w3c.dom.*;
5   import javax.xml.parsers.DocumentBuilderFactory;
6   import org.xml.sax.InputSource;
7   
8   
9   
10  public class ObtainSSLfromBML {
11  
12  	private static String XML_DECALRATION = "<?xml version="+'"'+"1.0"+'"'+ " encoding="+'"'+"ISO-8859-1"+'"'+" ?>";
13  	/***
14  	 * FOR TEST PROPOSES ONLY
15  	 * @param args
16  	 * @throws Exception 
17  	 */
18  	public static void main(String[] args) throws Exception {
19  
20  		//ObtainSSLfromBML ssl = new ObtainSSLfromBML();
21  		
22  		File myBMLFile = new File("resources/ZaragozaHotelModel.bml");
23  		String myBML = loadFile(myBMLFile);
24  		String mySSL = ObtainSSLfromBML.getSSLfromBML(myBML);
25  		ObtainSSLfromBML.saveFile(new File("C://","mySSL.xmi"),mySSL);
26  		//System.out.println(mySSL);
27  		
28  	}
29  	
30  	public static String loadFile(File file) throws Exception
31  	{
32  		FileInputStream fis = new FileInputStream(file);
33  		ByteArrayOutputStream baos = new ByteArrayOutputStream((int)file.length());
34  		
35  		byte[] b = new byte[4096];
36  		int nb;
37  		while ( (nb = fis.read(b)) != -1 ) {
38  			baos.write(b, 0, nb);
39  		}
40  		return baos.toString();
41  	}
42  	
43  	public static void saveFile(File file, String strContent) throws Exception
44  	{
45  		FileWriter fw = new FileWriter(file);
46  		fw.write(strContent);
47  		fw.close();
48  	}
49  	
50  	public static String getSSLfromBML(String bmlFile)throws Exception{
51  		
52  		DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
53  		dbFactory.setValidating(false);
54  		dbFactory.setIgnoringElementContentWhitespace(true);
55  		
56  		Document d = dbFactory.newDocumentBuilder().parse(new InputSource(new StringReader(bmlFile)));
57  		
58  		NodeList xmiNodes = d.getElementsByTagName("XMI");
59  		Node sslNode = xmiNodes.item(1);
60  		NodeList sslContent = sslNode.getChildNodes();
61  
62  		String sslFileString = sslContent.toString();
63  		sslFileString = XML_DECALRATION + "\n" + sslFileString;
64  		
65  		return sslFileString;
66  	}
67  
68  }