Skip to content

Instantly share code, notes, and snippets.

@nuzayats
Created March 23, 2015 06:15
Show Gist options
  • Save nuzayats/3fca75ff8e2ea691acde to your computer and use it in GitHub Desktop.
Save nuzayats/3fca75ff8e2ea691acde to your computer and use it in GitHub Desktop.
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.IOException;
import java.io.StringWriter;
public class PersistenceDescriptorUtils {
private static final String JAVAX_PERSISTENCE_SCHEMA_GENERATION_DATABASE_ACTION = "javax.persistence.schema-generation.database.action";
public static String setSchemaGenerationDatabaseAction(InputSource is, String action) throws ParserConfigurationException, IOException, SAXException, XPathExpressionException, TransformerException {
final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
final DocumentBuilder builder = factory.newDocumentBuilder();
final Document doc = builder.parse(is);
final XPathFactory xPathFactory = XPathFactory.newInstance();
final XPath xPath = xPathFactory.newXPath();
final Node persistenceUnit = (Node) xPath.evaluate("/persistence/persistence-unit", doc, XPathConstants.NODE);
if (persistenceUnit == null) {
// no persistence unit - do nothing
return toString(doc);
}
final Node properties = (Node) xPath.evaluate("properties", persistenceUnit, XPathConstants.NODE);
if (properties == null) {
// construct a properties element which contains a property element
persistenceUnit.appendChild(createPropertiesElement(doc, action));
return toString(doc);
}
final Node property = (Node) xPath.evaluate("property[@name='" + JAVAX_PERSISTENCE_SCHEMA_GENERATION_DATABASE_ACTION + "']", persistenceUnit, XPathConstants.NODE);
if (property != null) {
// delete the existing property element
properties.removeChild(property);
}
// add a property element
properties.appendChild(createPropertyElement(doc, action));
return toString(doc);
}
private static Element createPropertiesElement(Document doc, String action) {
final Element properties = doc.createElement("properties");
properties.appendChild(createPropertyElement(doc, action));
return properties;
}
private static Element createPropertyElement(Document doc, String action) {
final Element property = doc.createElement("property");
property.setAttribute("name", JAVAX_PERSISTENCE_SCHEMA_GENERATION_DATABASE_ACTION);
property.setAttribute("value", action);
return property;
}
private static String toString(Document doc) throws TransformerException, IOException {
final TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute("indent-number", 4);
final Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
try (final StringWriter sw = new StringWriter()) {
transformer.transform(new DOMSource(doc), new StreamResult(sw));
return sw.toString();
}
}
private PersistenceDescriptorUtils() {
}
}
import org.junit.Assert;
import org.junit.Test;
import org.xml.sax.InputSource;
import java.io.StringReader;
public class PersistenceDescriptorUtilsTest {
private static final String expected = "<property name=\"javax.persistence.schema-generation.database.action\" value=\"drop-and-create\"/>";
@Test
public void modify() throws Exception {
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<persistence xmlns=\"http://xmlns.jcp.org/xml/ns/persistence\" version=\"2.1\">\n" +
" <persistence-unit name=\"Model2PU\">\n" +
" <jta-data-source>java:jboss/datasources/Model2DS</jta-data-source>\n" +
" <properties>\n" +
" <property name=\"javax.persistence.schema-generation.database.action\" value=\"none\"/>\n" +
" <property name=\"someProp\" value=\"someValue\"/>\n" +
" </properties>\n" +
" </persistence-unit>\n" +
"</persistence>";
try (StringReader r = new StringReader(s)) {
Assert.assertTrue(PersistenceDescriptorUtils.setSchemaGenerationDatabaseAction(new InputSource(r), "drop-and-create").contains(expected));
}
}
@Test
public void add1() throws Exception {
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<persistence xmlns=\"http://xmlns.jcp.org/xml/ns/persistence\" version=\"2.1\">\n" +
" <persistence-unit name=\"Model2PU\">\n" +
" <jta-data-source>java:jboss/datasources/Model2DS</jta-data-source>\n" +
" <properties>\n" +
" <property name=\"someProp\" value=\"someValue\"/>\n" +
" </properties>\n" +
" </persistence-unit>\n" +
"</persistence>";
try (StringReader r = new StringReader(s)) {
final String s1 = PersistenceDescriptorUtils.setSchemaGenerationDatabaseAction(new InputSource(r), "drop-and-create");
System.out.println(s1);
Assert.assertTrue(s1.contains(expected));
}
}
@Test
public void add2() throws Exception {
String s = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<persistence xmlns=\"http://xmlns.jcp.org/xml/ns/persistence\" version=\"2.1\">\n" +
" <persistence-unit name=\"Model2PU\">\n" +
" <jta-data-source>java:jboss/datasources/Model2DS</jta-data-source>\n" +
" </persistence-unit>\n" +
"</persistence>";
try (StringReader r = new StringReader(s)) {
final String s1 = PersistenceDescriptorUtils.setSchemaGenerationDatabaseAction(new InputSource(r), "drop-and-create");
System.out.println(s1);
Assert.assertTrue(s1.contains(expected));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment