Author: rfelden
Date: Fri Jun 15 16:47:15 2007
New Revision: 60
URL: https://svndev.jahia.net/websvn/listing.php?sc=3D1&rev=3D60&repname=3D=
jahia_upgrade
Log:
merging...
Added:
trunk/apply/utils/src/main/java/org/jahia/utils/merge/
trunk/apply/utils/src/main/java/org/jahia/utils/merge/Merger.java
trunk/apply/utils/src/main/java/org/jahia/utils/merge/PropertiesMerger.=
java
trunk/apply/utils/src/main/java/org/jahia/utils/merge/XMLMerger.java
Added: trunk/apply/utils/src/main/java/org/jahia/utils/merge/Merger.java
URL: https://svndev.jahia.net/websvn/filedetails.php?path=3D/trunk/apply/ut=
ils/src/main/java/org/jahia/utils/merge/Merger.java&rev=3D60&repname=3Djahi=
a_upgrade
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/apply/utils/src/main/java/org/jahia/utils/merge/Merger.java (adde=
d)
+++ trunk/apply/utils/src/main/java/org/jahia/utils/merge/Merger.java Fri J=
un 15 16:47:15 2007
@@ -0,0 +1,23 @@
+package org.jahia.utils.merge;
+
+import java.io.File;
+
+/**
+ * @author Romain
+ * @version Jun 2007-06-14
+ */
+public class Merger {
+
+ public static boolean merge(final File update, final File old, final F=
ile existing) {
+ if (update.getName().endsWith(".xml")) {
+ return XMLMerger.merge(update, old, existing) ;
+ } else if (update.getName().endsWith(".properties")) {
+ return PropertiesMerger.merge(update, old, existing) ;
+ } else if (update.getName().endsWith(".plist")) {
+ return false ; // not implemented yet
+ } else {
+ return false ;
+ }
+ }
+
+}
Added: trunk/apply/utils/src/main/java/org/jahia/utils/merge/PropertiesMerg=
er.java
URL: https://svndev.jahia.net/websvn/filedetails.php?path=3D/trunk/apply/ut=
ils/src/main/java/org/jahia/utils/merge/PropertiesMerger.java&rev=3D60&repn=
ame=3Djahia_upgrade
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/apply/utils/src/main/java/org/jahia/utils/merge/PropertiesMerger.=
java (added)
+++ trunk/apply/utils/src/main/java/org/jahia/utils/merge/PropertiesMerger.=
java Fri Jun 15 16:47:15 2007
@@ -0,0 +1,77 @@
+package org.jahia.utils.merge;
+
+import org.apache.commons.configuration.*;
+import org.apache.commons.configuration.plist.PropertyListConfiguration;
+
+import java.io.File;
+import java.util.Iterator;
+
+/**
+ * This is the merge merger.
+ *
+ * @author Romain Felden
+ * @version 2007-06-12
+ */
+public class PropertiesMerger {
+
+ /**
+ * This static method uses three files to determine which merging has =
to be done
+ * in order to crete a hybrid merge file.
+ * It works with XML, .properties and .plist files.
+ *
+ * @param update the file that will be used to patch (will be modified=
if needed)
+ * @param old the standard previous version file
+ * @param existing the existing file
+ * @return true if the merging happened, false otherwise
+ */
+ public static boolean merge(final File update, final File old, final F=
ile existing) {
+
+ // check the files first
+ if (update =3D=3D null || !update.exists() ||
+ existing =3D=3D null || !existing.exists() ||
+ old =3D=3D null || !old.exists()) {
+ return false ;
+ }
+
+ try {
+ // load the configurations from the files
+ PropertiesConfiguration updateConfiguration =3D new Properties=
Configuration(update) ;
+ PropertiesConfiguration updateConfigCopy =3D new PropertiesCon=
figuration(update) ;
+ PropertiesConfiguration oldConfiguration =3D new PropertiesCon=
figuration(old) ;
+ PropertiesConfiguration existingConfiguration =3D new Properti=
esConfiguration(existing) ;
+ =
+ // set all the properties
+ Iterator it =3D updateConfigCopy.getKeys() ;
+ while (it.hasNext()) {
+ String key =3D it.next().toString() ;
+ Object updateValue =3D updateConfiguration.getProperty(key=
) ;
+ Object oldValue =3D oldConfiguration.getProperty(key) ;
+ Object existingValue =3D existingConfiguration.getProperty=
(key) ;
+
+ // if the key did not exist previously, neither in the ori=
ginal version
+ // nor at the client's, skip this property
+ // (the update value will be used)
+ if (oldValue =3D=3D null || existingValue =3D=3D null) {
+ }
+
+ // if the new value has not changed since last version,
+ // either keep the existing one if it exists, or leave it
+ // untouched in the updateConfiguration
+ else if (oldValue.equals(updateValue) && !existingValue.eq=
uals("")) {
+ updateConfiguration.setProperty(key, existingValue);
+ }
+ }
+
+ // save the data into the updater file
+ updateConfiguration.save(update) ;
+ } catch (ConfigurationException e) {
+ e.printStackTrace();
+ return false ;
+ } catch (NullPointerException npe) {
+ npe.printStackTrace() ;
+ }
+ return true ;
+
+ }
+
+}
Added: trunk/apply/utils/src/main/java/org/jahia/utils/merge/XMLMerger.java
URL: https://svndev.jahia.net/websvn/filedetails.php?path=3D/trunk/apply/ut=
ils/src/main/java/org/jahia/utils/merge/XMLMerger.java&rev=3D60&repname=3Dj=
ahia_upgrade
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- trunk/apply/utils/src/main/java/org/jahia/utils/merge/XMLMerger.java (a=
dded)
+++ trunk/apply/utils/src/main/java/org/jahia/utils/merge/XMLMerger.java Fr=
i Jun 15 16:47:15 2007
@@ -0,0 +1,198 @@
+package org.jahia.utils.merge;
+
+import org.jdom.*;
+import org.jdom.output.XMLOutputter;
+import org.jdom.output.Format;
+import org.jdom.input.SAXBuilder;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.FileWriter;
+import java.util.List;
+import java.util.Iterator;
+
+/**
+ * @author Romain Felden
+ * @version 2007-06-14
+ */
+public class XMLMerger {
+
+ public static boolean merge(final File update, final File old, final F=
ile existing) {
+ =
+ // check the files first
+ if (update =3D=3D null || !update.exists() ||
+ existing =3D=3D null || !existing.exists() ||
+ old =3D=3D null || !old.exists()) {
+ return false ;
+ }
+
+ try {
+ // load the configurations from the files
+ Document updateConfiguration =3D new SAXBuilder().build(update=
) ;
+ Document oldConfiguration =3D new SAXBuilder().build(old) ;
+ Document existingConfiguration =3D new SAXBuilder().build(exis=
ting) ;
+
+
+
+
+ // get root elements
+ Element updateRoot =3D updateConfiguration.getRootElement();
+ Element oldRoot =3D oldConfiguration.getRootElement();
+ Element existingRoot =3D existingConfiguration.getRootElement(=
);
+
+ // set all the properties
+ recMerge(updateRoot, oldRoot, existingRoot);
+
+ // save the data into the updater file
+ XMLOutputter outputter =3D new XMLOutputter(Format.getPrettyFo=
rmat()) ;
+ outputter.output(updateConfiguration, new FileWriter(update)) ;
+
+ } catch (IOException e) {
+ e.printStackTrace();
+ return false ;
+ } catch (JDOMException e) {
+ e.printStackTrace();
+ return false ;
+ }
+ return true ;
+ }
+
+ public static void recMerge(Element update, Element old, Element exist=
ing) {
+ =
+ // nothing to merge
+ if (existing =3D=3D null || old =3D=3D null) {
+ return ;
+ }
+
+
+
+ List updateChildList =3D update.getChildren() ;
+ int nbChild =3D updateChildList.size() ;
+
+ // recursive case
+ if (nbChild > 0) {
+
+ // do the merge on every child element (recursive)
+ for (int i =3D 0; i < nbChild; i++) {
+ Element updateChild =3D (Element) updateChildList.get(i) ;
+ String childName =3D updateChild.getName() ;
+
+ // retrieve the old child corresponding
+ List oldChildList =3D old.getChildren(childName) ;
+ Element oldChild =3D null ;
+ Iterator it =3D oldChildList.iterator() ;
+ while (it.hasNext()) {
+ Element temp =3D (Element) it.next() ;
+ if (areSameNodes(temp, updateChild)) {
+ oldChild =3D temp ;
+ break ;
+ }
+ }
+
+ // retrieve the old child corresponding
+ List existingChildList =3D existing.getChildren(childName)=
;
+ Element existingChild =3D null ;
+ Iterator it2 =3D existingChildList.iterator() ;
+ while (it2.hasNext()) {
+ Element temp =3D (Element) it2.next() ;
+ if (areSameNodes(temp, updateChild)) {
+ existingChild =3D temp ;
+ break ;
+ }
+ }
+
+
+ recMerge(updateChild, oldChild, existingChild) ;
+ if (oldChild !=3D null) {
+ old.removeChild(childName) ;
+ }
+ if (existingChild !=3D null) {
+ existing.removeChild(childName) ;
+ }
+ }
+
+ }
+
+ // leaf case
+ else {
+ List updateAttList =3D update.getAttributes() ;
+
+ // if there are attributes, compare values and set if needed
+ if (updateAttList.size() !=3D 0) {
+ for (int i =3D 0; i < updateAttList.size(); i++) {
+ Attribute updateAtt =3D (Attribute) updateAttList.get(=
i) ;
+ String attName =3D updateAtt.getName() ;
+ Attribute oldAtt =3D old.getAttribute(attName) ;
+ Attribute existingAtt =3D existing.getAttribute(attNam=
e) ;
+
+ // if the comparison has to be done
+ if (oldAtt !=3D null && existingAtt !=3D null) {
+ // get the att values
+ String updateValue =3D updateAtt.getValue() ;
+ String oldValue =3D oldAtt.getValue() ;
+ String existingValue =3D existingAtt.getValue() ;
+
+ // compare the att values
+ if (oldValue.equals(updateValue) && !existingValue=
.trim().equals("")) {
+ updateAtt.setValue(existingValue) ;
+ }
+ }
+ }
+ }
+
+ // then set the TEXT if existing
+ String updateText =3D update.getText().trim() ;
+ String oldText =3D old.getText().trim() ;
+ String existingText =3D existing.getText().trim() ;
+ if (oldText !=3D null && existingText !=3D null) {
+ // compare the text values
+ if (oldText.equals(updateText) && !existingText.trim().equ=
als("")) {
+ update.setText(existingText) ;
+ }
+ }
+ }
+ }
+
+ /**
+ * Checks if two given nodes have the same basic structure, that is sa=
me attributes.
+ *
+ * @param one a node as an Element
+ * @param two another node a an Element
+ * @return true if they do have the same structure, false otherwise.
+ */
+ public static boolean areSameNodes(Element one, Element two) {
+ // check existence
+ if (one =3D=3D null || two =3D=3D null) {
+ return false ;
+ }
+
+ // check names
+ if (!one.getName().equals(two.getName())) {
+ return false ;
+ }
+
+ // check att lists size
+ List oneAttList =3D one.getAttributes() ;
+ List twoAttList =3D two.getAttributes() ;
+ if (oneAttList.size() !=3D twoAttList.size()) {
+ return false ;
+ }
+
+ // check att names
+ boolean result =3D true ;
+ for (int i =3D 0; i < oneAttList.size() && result; i++) {
+ Attribute oneAtt =3D (Attribute)oneAttList.get(i) ;
+ result =3D two.getAttribute(oneAtt.getName()) !=3D null;
+ }
+
+ // check att values
+ for (int i =3D 0; i < oneAttList.size() && result; i++) {
+ Attribute oneAtt =3D (Attribute)oneAttList.get(i) ;
+ result =3D two.getAttributeValue(oneAtt.getName()).equals(oneA=
tt.getValue());
+ }
+
+ // they do have the same structure
+ return result ;
+
+ }
+}
_______________________________________________
cvs_list mailing list
[email protected]
http://lists.jahia.org/cgi-bin/mailman/listinfo/cvs_list