Skip to content

Commit 7f99225

Browse files
committed
Corrected wrong merge
1 parent f1cf67b commit 7f99225

1 file changed

Lines changed: 212 additions & 104 deletions

File tree

EDI-T/src/it/cnr/irea/ediT/MetadataTemplateDocument.java

Lines changed: 212 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Date;
2222
import java.util.HashMap;
2323
import java.util.List;
24+
import java.util.Stack;
2425
import java.util.regex.Matcher;
2526
import java.util.regex.Pattern;
2627

@@ -632,6 +633,7 @@ public void addElement(TemplateElement element) throws XPathExpressionException,
632633
System.err.println("on element " + nodes[j].substring(1));
633634
messages.add("DOM Exception on " + nodes[j].substring(1) + " - " + e.getLocalizedMessage());
634635
}
636+
// last.appendChild(output.createComment(comment));
635637
if ( item.getFixed() != null) {
636638
comment += "\nisFixed: " + item.getFixed();
637639
// last.setAttribute("ediFixed", item.getFixed());
@@ -640,7 +642,6 @@ public void addElement(TemplateElement element) throws XPathExpressionException,
640642
comment += "\nhasDatatype: " + item.getDataType();
641643
// last.setAttribute("ediDatatype", item.getDataType());
642644
}
643-
// last.appendChild(output.createComment(comment));
644645
} else {
645646
try {
646647
found = false;
@@ -1022,7 +1023,7 @@ public void addElementOld(TemplateElement element) throws XPathExpressionExcepti
10221023
System.err.println("on element " + nodes[j].substring(1));
10231024
messages.add("DOM Exception on " + nodes[j].substring(1) + " - " + e.getLocalizedMessage());
10241025
}
1025-
last.appendChild(output.createComment(comment));
1026+
// last.appendChild(output.createComment(comment));
10261027
if ( item.getFixed() != null) {
10271028
comment += "\nisFixed: " + item.getFixed();
10281029
// last.setAttribute("ediFixed", item.getFixed());
@@ -1188,6 +1189,143 @@ private Element createNode(String name, String value) {
11881189
return temp;
11891190
}
11901191

1192+
private Element setAttribute(Element base, String name, String value) {
1193+
if ( name.contains(":") ) {
1194+
UniversalNamespaceResolver resolver1 = new UniversalNamespaceResolver(output);
1195+
1196+
String prefix = name.substring(0, name.indexOf(":"));
1197+
String namespaceURI = resolver1.getNamespaceURI(prefix);
1198+
base.setAttributeNS(namespaceURI, name, value);
1199+
} else {
1200+
base.setAttribute(name, value);
1201+
}
1202+
return base;
1203+
}
1204+
1205+
private boolean hasAttribute(Element base, String name) {
1206+
if ( name.contains(":") ) {
1207+
UniversalNamespaceResolver resolver1 = new UniversalNamespaceResolver(output);
1208+
1209+
String prefix = name.substring(0, name.indexOf(":"));
1210+
String namespaceURI = resolver1.getNamespaceURI(prefix);
1211+
return base.hasAttributeNS(namespaceURI, name);
1212+
} else {
1213+
return base.hasAttribute(name);
1214+
}
1215+
}
1216+
1217+
private String getAttribute(Element base, String name) {
1218+
if ( name.contains(":") ) {
1219+
UniversalNamespaceResolver resolver1 = new UniversalNamespaceResolver(output);
1220+
1221+
String prefix = name.substring(0, name.indexOf(":"));
1222+
String namespaceURI = resolver1.getNamespaceURI(prefix);
1223+
return base.getAttributeNS(namespaceURI, name);
1224+
} else {
1225+
return base.getAttribute(name);
1226+
}
1227+
}
1228+
1229+
private String getXPath(Node node)
1230+
{
1231+
Node parent = node.getParentNode();
1232+
if (parent == null) {
1233+
return "/" + node.getNodeName();
1234+
}
1235+
return getXPath(parent) + "/";
1236+
}
1237+
1238+
public static String getFullXPath(Node n) {
1239+
// abort early
1240+
if (null == n)
1241+
return null;
1242+
1243+
// declarations
1244+
Node parent = null;
1245+
Stack<Node> hierarchy = new Stack<Node>();
1246+
StringBuffer buffer = new StringBuffer();
1247+
1248+
// push element on stack
1249+
hierarchy.push(n);
1250+
1251+
switch (n.getNodeType()) {
1252+
case Node.ATTRIBUTE_NODE:
1253+
parent = ((Attr) n).getOwnerElement();
1254+
break;
1255+
case Node.ELEMENT_NODE:
1256+
parent = n.getParentNode();
1257+
break;
1258+
case Node.DOCUMENT_NODE:
1259+
parent = n.getParentNode();
1260+
break;
1261+
default:
1262+
throw new IllegalStateException("Unexpected Node type" + n.getNodeType());
1263+
}
1264+
1265+
while (null != parent && parent.getNodeType() != Node.DOCUMENT_NODE) {
1266+
// push on stack
1267+
hierarchy.push(parent);
1268+
1269+
// get parent of parent
1270+
parent = parent.getParentNode();
1271+
}
1272+
1273+
// construct xpath
1274+
Object obj = null;
1275+
while (!hierarchy.isEmpty() && null != (obj = hierarchy.pop())) {
1276+
Node node = (Node) obj;
1277+
boolean handled = false;
1278+
1279+
if (node.getNodeType() == Node.ELEMENT_NODE) {
1280+
Element e = (Element) node;
1281+
1282+
// is this the root element?
1283+
if (buffer.length() == 0) {
1284+
// root element - simply append element name
1285+
buffer.append(node.getNodeName());
1286+
} else {
1287+
// child element - append slash and element name
1288+
buffer.append("/");
1289+
buffer.append(node.getNodeName());
1290+
1291+
if (node.hasAttributes()) {
1292+
// see if the element has a name or id attribute
1293+
if (e.hasAttribute("id")) {
1294+
// id attribute found - use that
1295+
buffer.append("[@id='" + e.getAttribute("id") + "']");
1296+
handled = true;
1297+
} else if (e.hasAttribute("name")) {
1298+
// name attribute found - use that
1299+
buffer.append("[@name='" + e.getAttribute("name") + "']");
1300+
handled = true;
1301+
}
1302+
}
1303+
1304+
if (!handled) {
1305+
// no known attribute we could use - get sibling index
1306+
int prev_siblings = 1;
1307+
Node prev_sibling = node.getPreviousSibling();
1308+
while (null != prev_sibling) {
1309+
if (prev_sibling.getNodeType() == node.getNodeType()) {
1310+
if (prev_sibling.getNodeName().equalsIgnoreCase(
1311+
node.getNodeName())) {
1312+
prev_siblings++;
1313+
}
1314+
}
1315+
prev_sibling = prev_sibling.getPreviousSibling();
1316+
}
1317+
buffer.append("[" + prev_siblings + "]");
1318+
}
1319+
}
1320+
} else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
1321+
buffer.append("/@");
1322+
buffer.append(node.getNodeName());
1323+
}
1324+
}
1325+
// return buffer
1326+
return buffer.toString();
1327+
}
1328+
11911329
/**
11921330
* Creates the or edit root node.
11931331
*
@@ -1197,118 +1335,88 @@ private Element createNode(String name, String value) {
11971335
* @return the element
11981336
*/
11991337
public Element createOrEditRootNode(Element element, String path, String value) {
1200-
Element temp = null;
1201-
boolean found = false;
1202-
boolean nodeCreated = false;
1203-
1204-
XPathFactory xPathfactory = XPathFactory.newInstance();
1205-
XPath xpath = xPathfactory.newXPath();
1206-
if ( path.length() <= 0 ) {
1207-
return null;
1208-
}
1209-
String[] nodes = path.substring(1).split("\\/");
1210-
Pattern pattern = Pattern.compile(attributeRegex);
1211-
Matcher matcher;
1212-
String node = "dummy";;
1213-
String attribute = null;
1214-
String val = null;
1215-
1216-
System.out.println("createOrEditRootNode: " + path);
1217-
1218-
XPathExpression expr;
1219-
if ( nodes.length > 0 && !nodes[0].trim().equalsIgnoreCase("")) {
1220-
// System.out.println("nodes[0] = " + nodes[0]);
1221-
node = nodes[0];
1222-
for ( int i = 0; element != null && element.getChildNodes() != null && i < element.getChildNodes().getLength(); i++ ) {
1223-
if ( node.contains("@") ) {
1224-
// It's an attribute
1225-
node = node.replace("\"", "'");
1226-
matcher = pattern.matcher(node);
1227-
if ( matcher.find() ) {
1228-
node = node.replaceAll("\\[.*\\]", "");
1229-
attribute = matcher.group(1);
1230-
val = matcher.group(2);
1231-
System.out.println("createOrEditRootNode: @" + attribute + "='" + val + "'");
1232-
if ( element.getChildNodes().item(i).getNodeName().equalsIgnoreCase(node) ) {
1233-
if ( element.getChildNodes().item(i).getAttributes().getLength() > 0 ) {
1234-
for ( int j = 0; j < element.getChildNodes().item(i).getAttributes().getLength(); j++ ) {
1235-
Node foundAttribute = element.getChildNodes().item(i).getAttributes().getNamedItem(attribute);
1236-
if ( foundAttribute != null && foundAttribute.getNodeName().equalsIgnoreCase(attribute) && foundAttribute.getNodeValue().equalsIgnoreCase(val) ) {
1237-
System.out.println("createOrEditRootNode: @" + attribute + "='" + val + "' already in place");
1238-
found = true;
1239-
temp = (Element) element.getChildNodes().item(i);
1240-
break;
1241-
}
1242-
}
1243-
}
1244-
// TODO: verificare 'sto break
1245-
break;
1246-
}
1247-
}
1338+
banner(path + " -> " + value);
1339+
// System.out.println(xmlString(element));
1340+
banner("Working on " + getFullXPath((Node) element));
1341+
Element temp = element;
1342+
ArrayList<PathElement> pathElements = PathElement.parsePath(path);
1343+
for ( int j = 0; j < Math.min(1, pathElements.size()); j++ ) {
1344+
PathElement p = pathElements.get(j);
1345+
if ( p.isAttribute() ) {
1346+
if ( hasAttribute(element, p.getName()) && getAttribute(element, p.getName()).equalsIgnoreCase(p.getValue()) ) {
1347+
// nothing to do
12481348
} else {
1249-
if ( element.getChildNodes().item(i).getNodeName().equalsIgnoreCase(nodes[0]) ) {
1250-
found = true;
1251-
temp = (Element) element.getChildNodes().item(i);
1252-
break;
1349+
if ( !hasAttribute(element, p.getName()) ) {
1350+
setAttribute(element, p.getName(), p.getValue());
1351+
banner("setting attribute " + p.getName() + " to " + p.getValue());
1352+
} else {
1353+
Element e = createNode(temp.getNodeName(), temp.getTextContent());
1354+
banner("creating sibling node " + p.getName() + " to " + p.getValue());
1355+
temp.getParentNode().appendChild(e);
1356+
temp = e;
1357+
banner("with attribute " + p.getName() + " to " + p.getValue());
1358+
temp = setAttribute(temp, p.getName(), p.getValue());
12531359
}
12541360
}
1255-
}
1256-
if ( !found ) {
1257-
node = node.replace("\n", "");
1258-
if ( node.contains("[") ) {
1259-
// It's an attribute
1260-
node = node.replace("\"", "'");
1261-
matcher = pattern.matcher(node);
1262-
node = node.replaceAll("\\[.*\\]", "");
1263-
attribute = "";
1264-
val="";
1265-
if ( matcher.find() ) {
1266-
attribute = matcher.group(1);
1267-
val = matcher.group(2);
1361+
} else {
1362+
// it's a node
1363+
boolean found = false;
1364+
1365+
for ( int i = 0; !found && i < element.getChildNodes().getLength(); i++ ) {
1366+
banner("Comparing " + element.getChildNodes().item(i).getNodeName() + " to " + p.getName());
1367+
if ( element.getChildNodes().item(i).getNodeName().equalsIgnoreCase(p.getName()) ) {
1368+
banner("node " + p.getName() + " found");
1369+
if ( PathElement.isNextAnAttribute(pathElements, j) ) {
1370+
PathElement pAttribute = pathElements.get(j+1);
1371+
if ( ((Element)element.getChildNodes().item(i)).hasAttribute(pAttribute.getName()) ) {
1372+
if ( !getAttribute((Element)element.getChildNodes().item(i), pAttribute.getName()).equalsIgnoreCase(pAttribute.getValue()) ) {
1373+
found = false;
1374+
banner("1");
1375+
/*
1376+
Element e = createNode(temp.getNodeName(), temp.getTextContent());
1377+
temp.getParentNode().appendChild(e);
1378+
temp = e;
1379+
temp = setAttribute(temp, p.getName(), p.getValue());
1380+
*/
1381+
} else {
1382+
found = true;
1383+
temp = (Element) element.getChildNodes().item(i);
1384+
}
1385+
} else {
1386+
banner("2");
1387+
found = true;
1388+
temp = (Element) element.getChildNodes().item(i);
1389+
temp.setAttribute(pAttribute.getName(), pAttribute.getValue());
1390+
}
1391+
} else {
1392+
banner("3");
1393+
temp = (Element) element.getChildNodes().item(i);
1394+
found = true;
1395+
}
12681396
}
12691397
}
1270-
System.out.println("creating node " + node + ", " + attribute + "=" + val);
1271-
String prefix = "";
1272-
String namespaceURI = "";
1273-
if ( node.contains(":") ) {
1274-
UniversalNamespaceResolver resolver1 = new UniversalNamespaceResolver(output);
1398+
if ( !found ) {
1399+
temp = createNode(p.getName(), value);
1400+
banner("appending " + p.getName());
1401+
if ( PathElement.isNextAnAttribute(pathElements, j) ) {
1402+
PathElement pAttribute = pathElements.get(j+1);
1403+
banner("with attribute " + pAttribute.getName() + " to " + pAttribute.getValue());
12751404

1276-
prefix = node.substring(0, node.indexOf(":"));
1277-
// System.out.println("prefix: " + prefix);
1278-
namespaceURI = resolver1.getNamespaceURI(prefix);
1279-
try {
1280-
temp = output.createElementNS(namespaceURI, node);
1281-
} catch (DOMException e) {
1282-
System.err.println(namespaceURI + " - " + node + " " + e.getLocalizedMessage());
1283-
throw e;
1405+
temp = setAttribute(temp, pAttribute.getName(), pAttribute.getValue());
12841406
}
1285-
} else {
1286-
try {
1287-
temp = output.createElement(node);
1288-
} catch (DOMException e) {
1289-
System.err.println(" - " + node + " " + e.getLocalizedMessage());
1290-
throw e;
1407+
if ( value != null && PathElement.isLastNode(pathElements, j)) {
1408+
temp.setTextContent(value);
12911409
}
1410+
element.appendChild(temp);
1411+
12921412
}
1293-
if ( attribute != null && val != null ) {
1294-
temp.setAttribute(attribute, val);
1295-
}
1296-
element.appendChild(temp);
1297-
}
1298-
if ( nodes.length > 1 ) {
1299-
String newPath = "";
1300-
for ( int i = 1; i < nodes.length; i++ ) {
1301-
newPath += "/" + nodes[i];
1302-
}
1303-
createOrEditRootNode(temp, newPath, value);
1304-
} else {
1305-
// temp.setTextContent(value);
13061413
}
1307-
} else {
1308-
return null;
1414+
banner("pathElements before: " + PathElement.toString(pathElements));
1415+
pathElements.remove(0);
1416+
banner("pathElements after: " + PathElement.toString(pathElements));
1417+
createOrEditRootNode(temp, PathElement.toString(pathElements), value);
1418+
13091419
}
1310-
// System.err.println();
1311-
// System.err.println(xmlString(temp));
13121420
return temp;
13131421
}
13141422

@@ -1353,4 +1461,4 @@ private static String getImplementationInfo(String componentName, Class componen
13531461
public List<String> getMessages() {
13541462
return messages;
13551463
}
1356-
}
1464+
}

0 commit comments

Comments
 (0)