Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement