View Javadoc

1   package org.ocltf.utils;
2   
3   import java.io.ByteArrayInputStream;
4   import java.io.InputStream;
5   import java.io.StringWriter;
6   
7   import javax.xml.parsers.DocumentBuilder;
8   import javax.xml.parsers.DocumentBuilderFactory;
9   import javax.xml.transform.Transformer;
10  import javax.xml.transform.TransformerFactory;
11  import javax.xml.transform.dom.DOMSource;
12  import javax.xml.transform.stream.StreamResult;
13  
14  import org.apache.commons.lang.StringUtils;
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.w3c.dom.Document;
18  import org.w3c.dom.Node;
19  import org.w3c.dom.NodeList;
20  
21  /***
22   * Contains utilities for modifying/processing XMI files.
23   * 
24   * @author Chad Brandon
25   */
26  public class XmiUtils {
27  	
28  	private static final Log logger = LogFactory.getLog(XmiUtils.class);
29  	
30  	private static final String TRANSFORMER_FACTORY = "javax.xml.transform.TransformerFactory";
31  	private static final String TRANSFORMER_FACTORY_IMPL = "org.apache.xalan.processor.TransformerFactoryImpl";
32  	
33  	static {
34  		if (StringUtils.isBlank(System.getProperty(TRANSFORMER_FACTORY))) {
35  			//set the default TransformerFactory if for some reason it wasn't 
36  			//set already.
37  			System.setProperty(
38  				TRANSFORMER_FACTORY, 
39  				TRANSFORMER_FACTORY_IMPL);	
40  			if (logger.isDebugEnabled()) {
41  				logger.debug("system property '" 
42  					+ TRANSFORMER_FACTORY 
43  					+ "' isn't set, setting to default --> " + TRANSFORMER_FACTORY_IMPL);
44  			}		
45  		}
46  	}
47  
48  	/***
49  	 * Strips elements from the passed in modelXmi and returns the model
50  	 * with the removed elements as an InputStream
51  	 * @param xmiStream the InputStream for the XMI. 
52  	 * @param elementNames
53  	 * @return InputStream after the elements are stripped.
54  	 */
55  	public static InputStream stripElements(InputStream xmiStream, String[] elementNames) {
56  		String methodName = "stripElements";
57  		if (logger.isDebugEnabled()) {
58  			logger.debug("performing " + methodName 
59  				+ " with xmiStream (" + xmiStream + ") and elementNames (" + elementNames + ")");
60  		}
61  		ExceptionUtils.checkNull(methodName, "xmiStream", xmiStream);
62  		ExceptionUtils.checkNull(methodName, "elementNames", elementNames);
63  		try {
64  			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
65  			DocumentBuilder builder = factory.newDocumentBuilder();
66  					
67  			long start = System.currentTimeMillis();
68  
69  			Document document = builder.parse(xmiStream);
70  
71  			if (logger.isDebugEnabled()) {
72  				logger.debug("completed reading XMI, TIME --> " 
73  					+ (System.currentTimeMillis() - start)/1000.0 + "[s]");
74  			}
75  	
76  			for (int ctr = 0; ctr < elementNames.length; ctr++) {
77  				String name = elementNames[ctr];
78  				if (StringUtils.isNotBlank(name)) {
79  					removeElements(document, StringUtils.trim(elementNames[ctr]));			
80  				}		
81  			}
82  		
83  			Transformer serializer =
84  				TransformerFactory.newInstance().newTransformer();
85  			StringWriter sw = new StringWriter();
86  		
87  			serializer.transform(new DOMSource(document), new StreamResult(sw));
88  					
89  			ByteArrayInputStream stream = new ByteArrayInputStream(sw.toString().getBytes());
90  
91  			if (logger.isDebugEnabled()) {
92  				logger.debug("completed methodName with stream --> " + stream);
93  			}	
94  			
95  			return stream;	
96  		} catch (Exception ex) {
97  			String errMsg = "Error performing " + methodName;
98  			logger.error(errMsg, ex);
99  			throw new XmiUtilsException(errMsg, ex);
100 		}
101 	}
102 	
103 	/***
104 	 * Removes the specified element name for the document.
105 	 * @param document the Document from which to remove the element having the 
106 	 *        given name.
107 	 * @param elementName the name of the element for which to remove if it can
108 	 *        be found.
109 	 */
110 	protected static void removeElements(Document document, String elementName) {
111 		String methodName = "removeElements";
112 		ExceptionUtils.checkNull(methodName, "document", document);
113 		ExceptionUtils.checkEmpty(methodName, "elementName", elementName);
114 		try {
115 				
116 			NodeList list = document.getElementsByTagName(elementName);
117 			for (int ctr = 0; ctr < list.getLength(); ctr++) {
118 				Node node = list.item(ctr);
119 				if (node != null) {
120 					Node parent = node.getParentNode();
121 					if (parent != null) {
122 						parent.removeChild(node);
123 					}
124 					removeElements(document, elementName);
125 				}
126 			}
127 			
128 		} catch (Exception ex) {
129 			String errMsg = "Error performing " + methodName;
130 			logger.error(errMsg, ex);
131 			throw new XmiUtilsException(errMsg, ex);
132 		}
133 	}
134 
135 }