Advertisement
Guest User

Untitled

a guest
May 6th, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. import org.w3c.dom.Document;
  2. import org.w3c.dom.Element;
  3. import org.w3c.dom.Node;
  4. import org.xml.sax.InputSource;
  5. import org.xml.sax.SAXException;
  6.  
  7. import javax.xml.parsers.DocumentBuilder;
  8. import javax.xml.parsers.DocumentBuilderFactory;
  9. import javax.xml.parsers.ParserConfigurationException;
  10. import javax.xml.transform.OutputKeys;
  11. import javax.xml.transform.Transformer;
  12. import javax.xml.transform.TransformerException;
  13. import javax.xml.transform.TransformerFactory;
  14. import javax.xml.transform.dom.DOMSource;
  15. import javax.xml.transform.stream.StreamResult;
  16. import javax.xml.xpath.XPath;
  17. import javax.xml.xpath.XPathConstants;
  18. import javax.xml.xpath.XPathExpressionException;
  19. import javax.xml.xpath.XPathFactory;
  20. import java.io.IOException;
  21. import java.io.StringWriter;
  22.  
  23. public class PersistenceDescriptorUtils {
  24.  
  25. private static final String JAVAX_PERSISTENCE_SCHEMA_GENERATION_DATABASE_ACTION = "javax.persistence.schema-generation.database.action";
  26.  
  27. public static String setSchemaGenerationDatabaseAction(InputSource is, String action) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException, TransformerException {
  28. final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  29. final DocumentBuilder builder = factory.newDocumentBuilder();
  30. final Document doc = builder.parse(is);
  31.  
  32. final XPathFactory xPathFactory = XPathFactory.newInstance();
  33. final XPath xPath = xPathFactory.newXPath();
  34.  
  35. final Node persistenceUnit = (Node) xPath.evaluate("/persistence/persistence-unit", doc, XPathConstants.NODE);
  36. if (persistenceUnit == null) {
  37. // no persistence unit - do nothing
  38. return toString(doc);
  39. }
  40.  
  41. final Node properties = (Node) xPath.evaluate("properties", persistenceUnit, XPathConstants.NODE);
  42. if (properties == null) {
  43. // construct a properties element which contains a property element
  44. persistenceUnit.appendChild(createPropertiesElement(doc, action));
  45. return toString(doc);
  46. }
  47.  
  48. final Node property = (Node) xPath.evaluate("property[@name='" + JAVAX_PERSISTENCE_SCHEMA_GENERATION_DATABASE_ACTION + "']", persistenceUnit, XPathConstants.NODE);
  49. if (property != null) {
  50. // delete the existing property element
  51. properties.removeChild(property);
  52. }
  53.  
  54. // add a property element
  55. properties.appendChild(createPropertyElement(doc, action));
  56. return toString(doc);
  57. }
  58.  
  59. private static Element createPropertiesElement(Document doc, String action) {
  60. final Element properties = doc.createElement("properties");
  61. properties.appendChild(createPropertyElement(doc, action));
  62. return properties;
  63. }
  64.  
  65. private static Element createPropertyElement(Document doc, String action) {
  66. final Element property = doc.createElement("property");
  67. property.setAttribute("name", JAVAX_PERSISTENCE_SCHEMA_GENERATION_DATABASE_ACTION);
  68. property.setAttribute("value", action);
  69. return property;
  70. }
  71.  
  72. private static String toString(Document doc) throws TransformerException, IOException {
  73. final TransformerFactory factory = TransformerFactory.newInstance();
  74. factory.setAttribute("indent-number", 4);
  75. final Transformer transformer = factory.newTransformer();
  76. transformer.setOutputProperty(OutputKeys.INDENT, "yes");
  77. transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
  78. try (final StringWriter sw = new StringWriter()) {
  79. transformer.transform(new DOMSource(doc), new StreamResult(sw));
  80. return sw.toString();
  81. }
  82. }
  83.  
  84. private PersistenceDescriptorUtils() {
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement