Skip to content

Commit

Permalink
Issue #341 - DOM access synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
pavels committed Feb 22, 2016
1 parent 5366549 commit d5dde5b
Show file tree
Hide file tree
Showing 4 changed files with 254 additions and 195 deletions.
16 changes: 8 additions & 8 deletions common/src/main/java/cz/incad/kramerius/impl/SolrAccessImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,15 @@ public ObjectModelsPath[] getPathOfModels(String pid) throws IOException {

private ObjectModelsPath[] getPathOfModels(Document doc)
throws XPathExpressionException {
List<String> disected = SolrUtils.disectModelPaths(doc);
ObjectModelsPath[] paths = new ObjectModelsPath[disected.size()];
for (int i = 0; i < paths.length; i++) {
String[] models = disected.get(i).split("/");
paths[i] = new ObjectModelsPath(models);
synchronized(doc) {
List<String> disected = SolrUtils.disectModelPaths(doc);
ObjectModelsPath[] paths = new ObjectModelsPath[disected.size()];
for (int i = 0; i < paths.length; i++) {
String[] models = disected.get(i).split("/");
paths[i] = new ObjectModelsPath(models);
}
return paths;
}
return paths;
}


Expand Down Expand Up @@ -214,6 +216,4 @@ public InputStream terms(String req, String type) throws IOException {
throw new IOException(e);
}
}


}
213 changes: 122 additions & 91 deletions common/src/main/java/cz/incad/kramerius/utils/XMLUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.Collections;
import java.util.List;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand All @@ -29,12 +31,17 @@
import org.xml.sax.SAXException;

/**
* Simple xml utitlities
* Handle XML; parsing, find, etc..
* @author pavels
*
* <b>Implementation note: All DOM access methods are synchronized on Document object </b>
*
*/
public class XMLUtils {

public static final Logger LOGGER = Logger.getLogger(XMLUtils.class.getName());


/**
* PArse document from reader
* @param reader Reader
Expand All @@ -44,7 +51,9 @@ public class XMLUtils {
* @throws IOException
*/
public static Document parseDocument(Reader reader) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();
LOGGER.log(Level.FINE, "builder factory instance :"+newInstance.getClass().getResource(newInstance.getClass().getSimpleName()+".class"));
DocumentBuilder builder = newInstance.newDocumentBuilder();
return builder.parse(new InputSource(reader));
}

Expand All @@ -59,6 +68,7 @@ public static Document parseDocument(Reader reader) throws ParserConfigurationEx
*/
public static Document parseDocument(Reader reader, boolean namespaceaware) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
LOGGER.log(Level.FINE, "builder factory instance :"+factory.getClass().getResource(factory.getClass().getSimpleName()+".class"));
factory.setNamespaceAware(namespaceaware);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(new InputSource(reader));
Expand All @@ -73,7 +83,9 @@ public static Document parseDocument(Reader reader, boolean namespaceaware) thro
* @throws IOException
*/
public static Document parseDocument(InputStream is) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
DocumentBuilderFactory newInstance = DocumentBuilderFactory.newInstance();
LOGGER.log(Level.FINE, "builder factory instance :"+newInstance.getClass().getResource(newInstance.getClass().getSimpleName()+".class"));
DocumentBuilder builder = newInstance.newDocumentBuilder();
return builder.parse(is);
}

Expand All @@ -88,6 +100,7 @@ public static Document parseDocument(InputStream is) throws ParserConfigurationE
*/
public static Document parseDocument(InputStream is, boolean namespaceaware) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
LOGGER.log(Level.FINE, "builder factory instance :"+factory.getClass().getResource(factory.getClass().getSimpleName()+".class"));
factory.setNamespaceAware(namespaceaware);
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(is);
Expand All @@ -99,15 +112,18 @@ public static Document parseDocument(InputStream is, boolean namespaceaware) thr
* @return
*/
public static List<Element> getElements(Element topElm) {
List<Element> retVals = new ArrayList<Element>();
NodeList childNodes = topElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node n = childNodes.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
retVals.add((Element) n);
if (topElm == null) throw new IllegalArgumentException("topElm cannot be null");
synchronized(topElm.getOwnerDocument()) {
List<Element> retVals = new ArrayList<Element>();
NodeList childNodes = topElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node n = childNodes.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
retVals.add((Element) n);
}
}
return retVals;
}
return retVals;
}

/**
Expand All @@ -117,38 +133,44 @@ public static List<Element> getElements(Element topElm) {
* @return
*/
public static List<Element> getElements(Element topElm, ElementsFilter filter ) {
List<Element> retVals = new ArrayList<Element>();
NodeList childNodes = topElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node n = childNodes.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element elm = (Element) n;
if (filter.acceptElement(elm)) {
retVals.add(elm);
if (topElm == null) throw new IllegalArgumentException("topElm cannot be null");
synchronized(topElm.getOwnerDocument()) {
List<Element> retVals = new ArrayList<Element>();
NodeList childNodes = topElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node n = childNodes.item(i);
if (n.getNodeType() == Node.ELEMENT_NODE) {
Element elm = (Element) n;
if (filter.acceptElement(elm)) {
retVals.add(elm);
}
}
}
return retVals;
}
return retVals;
}

public static List<Element> getElementsRecursive(Element topElm, ElementsFilter filter) {
List<Element> elms = new ArrayList<Element>();
Stack<Element> stack = new Stack<Element>();
stack.push(topElm);
while (!stack.isEmpty()) {
Element curElm = stack.pop();
if (filter.acceptElement(curElm)) {
elms.add(curElm);
}
NodeList childNodes = curElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node item = childNodes.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
stack.push((Element) item);
if (topElm == null) throw new IllegalArgumentException("topElm cannot be null");
synchronized(topElm.getOwnerDocument()) {
List<Element> elms = new ArrayList<Element>();
Stack<Element> stack = new Stack<Element>();
stack.push(topElm);
while (!stack.isEmpty()) {
Element curElm = stack.pop();
if (filter.acceptElement(curElm)) {
elms.add(curElm);
}
NodeList childNodes = curElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node item = childNodes.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
stack.push((Element) item);
}
}
}
return elms;
}
return elms;
}


Expand All @@ -168,29 +190,32 @@ private static boolean namespacesAreSame(String fNamespace, String sNamespace) {
* @return returns found node
*/
public static Element findElement(Element topElm, String nodeName) {
Stack<Element> stack = new Stack<Element>();
stack.push(topElm);
while (!stack.isEmpty()) {
Element curElm = stack.pop();
if (curElm.getNodeName().equals(nodeName)) {
return curElm;
}
List<Node> nodesToProcess = new ArrayList<Node>();
if (topElm == null) throw new IllegalArgumentException("topElm cannot be null");
synchronized(topElm.getOwnerDocument()) {
Stack<Element> stack = new Stack<Element>();
stack.push(topElm);
while (!stack.isEmpty()) {
Element curElm = stack.pop();
if (curElm.getNodeName().equals(nodeName)) {
return curElm;
}
List<Node> nodesToProcess = new ArrayList<Node>();

NodeList childNodes = curElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node item = childNodes.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
//stack.push((Element) item);
nodesToProcess.add(item);
NodeList childNodes = curElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node item = childNodes.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
//stack.push((Element) item);
nodesToProcess.add(item);
}
}
Collections.reverse(nodesToProcess);
for (Node node : nodesToProcess) {
stack.push((Element) node);
}
}
Collections.reverse(nodesToProcess);
for (Node node : nodesToProcess) {
stack.push((Element) node);
}
return null;
}
return null;
}

/**
Expand All @@ -201,57 +226,63 @@ public static Element findElement(Element topElm, String nodeName) {
* @return found element
*/
public static Element findElement(Element topElm, String localName, String namespace) {
Stack<Element> stack = new Stack<Element>();
stack.push(topElm);
while (!stack.isEmpty()) {
Element curElm = stack.pop();
if ((curElm.getLocalName().equals(localName)) && (namespacesAreSame(curElm.getNamespaceURI(), namespace))) {
return curElm;
}
List<Node> nodesToProcess = new ArrayList<Node>();
NodeList childNodes = curElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node item = childNodes.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
nodesToProcess.add(item);
if (topElm == null) throw new IllegalArgumentException("topElm cannot be null");
synchronized(topElm.getOwnerDocument()) {
Stack<Element> stack = new Stack<Element>();
stack.push(topElm);
while (!stack.isEmpty()) {
Element curElm = stack.pop();
if ((curElm.getLocalName().equals(localName)) && (namespacesAreSame(curElm.getNamespaceURI(), namespace))) {
return curElm;
}
List<Node> nodesToProcess = new ArrayList<Node>();
NodeList childNodes = curElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node item = childNodes.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
nodesToProcess.add(item);
}
}
// because of stack
Collections.reverse(nodesToProcess);
for (Node node : nodesToProcess) {
stack.push((Element) node);

}
}
// because of stack
Collections.reverse(nodesToProcess);
for (Node node : nodesToProcess) {
stack.push((Element) node);

}
return null;
}
return null;
}



public static Element findElement(Element topElm, ElementsFilter filter) {
Stack<Element> stack = new Stack<Element>();
stack.push(topElm);
while (!stack.isEmpty()) {
Element curElm = stack.pop();
if (filter.acceptElement(curElm)) {
return curElm;
}
if (topElm == null) throw new IllegalArgumentException("topElm cannot be null");
synchronized(topElm.getOwnerDocument()) {
Stack<Element> stack = new Stack<Element>();
stack.push(topElm);
while (!stack.isEmpty()) {
Element curElm = stack.pop();
if (filter.acceptElement(curElm)) {
return curElm;
}

List<Node> nodesToProcess = new ArrayList<Node>();
NodeList childNodes = curElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node item = childNodes.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
//stack.push((Element) item);
nodesToProcess.add(item);
List<Node> nodesToProcess = new ArrayList<Node>();
NodeList childNodes = curElm.getChildNodes();
for (int i = 0, ll = childNodes.getLength(); i < ll; i++) {
Node item = childNodes.item(i);
if (item.getNodeType() == Node.ELEMENT_NODE) {
//stack.push((Element) item);
nodesToProcess.add(item);
}
}
Collections.reverse(nodesToProcess);
for (Node node : nodesToProcess) {
stack.push((Element)node);
}
}
Collections.reverse(nodesToProcess);
for (Node node : nodesToProcess) {
stack.push((Element)node);
}
return null;
}
return null;
}


Expand Down
Loading

0 comments on commit d5dde5b

Please sign in to comment.