Changeset 2243


Ignore:
Timestamp:
02/07/07 18:02:54 (13 years ago)
Author:
chris
Message:

Notebook:
Companion files and merging completely in place, should
work given well given the testing I've done so far. It
seems that you make new Elements and Junk through the
Document object itself which makes a crazy sort of sense
since they are irrevocably bound to that object. The one
thing that's not really supported here are internal
semantic type definitions, and I'm not sure if that's a
priority or not.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/loci/ome/notebook/Merger.java

    r2214 r2243  
    3131import javax.swing.JOptionPane; 
    3232import java.io.FileInputStream; 
     33import java.util.Vector; 
    3334 
    3435public class Merger { 
     
    7980  } 
    8081   
     82  /** 
     83  * Merge two OME-XML Trees, when a conflict arrises, use the 
     84  * "over" tree's node instead of the "under" tree's. NB: the 
     85  * trees should be in OMECA (flattened) format before passing 
     86  * them to this method. 
     87  * @param over The OMENode that by default has higher priority. 
     88  * @param under The OMENode that by default has lower priority. 
     89  */ 
    8190  public static OMENode merge(OMENode over, OMENode under) { 
    82     return over; 
     91    OMENode result; 
     92    OMEXMLNode tempNode = merge((OMEXMLNode)over,(OMEXMLNode)under); 
     93    if (tempNode instanceof OMENode) result = (OMENode) tempNode; 
     94    else result = null; 
     95    return result; 
     96  } 
     97     
     98  private static OMEXMLNode merge(OMEXMLNode over, OMEXMLNode under) { 
     99    OMEXMLNode result = over; 
     100    Vector overList = result.getChildren(); 
     101    Vector underList = under.getChildren(); 
     102    Vector idList = new Vector(); 
     103    boolean isOverCustom = false; 
     104    boolean isUnderCustom = false; 
     105    boolean addedCustom = false; 
     106     
     107    for(int i = 0;i<overList.size();i++) { 
     108      OMEXMLNode overNode = (OMEXMLNode)(overList.get(i)); 
     109      String overID = overNode.getAttribute("ID"); 
     110      if (overID == null) isOverCustom = true; 
     111 
     112      for(int j = 0;j<underList.size();j++) { 
     113        OMEXMLNode underNode = (OMEXMLNode)(underList.get(j)); 
     114        String underID = underNode.getAttribute("ID"); 
     115        if (underID == null) isUnderCustom = true; 
     116         
     117        if(isOverCustom && !isUnderCustom) { 
     118          //do nothing to alter custom tree 
     119          isOverCustom = false; 
     120        } 
     121        else if(!isOverCustom && isUnderCustom && !addedCustom) { 
     122          result.getDOMElement().appendChild(createClone( 
     123            underNode.getDOMElement(),overNode.getDOMElement(). 
     124            getOwnerDocument())); 
     125          addedCustom = true; 
     126          isUnderCustom = false; 
     127        } 
     128        else if (!isOverCustom && !isUnderCustom) { 
     129          if (underID.equals(overID)) 
     130            merge(overNode,underNode); 
     131          else { 
     132            if(idList.indexOf(underID) > -1) { 
     133              result.getDOMElement().appendChild(createClone( 
     134                underNode.getDOMElement(),overNode.getDOMElement(). 
     135                getOwnerDocument())); 
     136              idList.add(underID); 
     137            } 
     138          } 
     139        } 
     140      } 
     141    } 
     142     
     143    return result; 
     144  } 
     145   
     146  public static Element createClone(Element el, Document doc) { 
     147    String tagName = el.getTagName(); 
     148    Element clone = doc.createElement(tagName); 
     149 
     150    if(el.hasAttributes()) { 
     151      NamedNodeMap map = el.getAttributes(); 
     152      for(int i = 0;i<map.getLength();i++) { 
     153        Node thisAttr = map.item(i); 
     154        String attrName = thisAttr.getNodeName(); 
     155        String attrValue = thisAttr.getNodeValue(); 
     156        clone.setAttribute(attrName,attrValue); 
     157      } 
     158    } 
     159     
     160    if(el.hasChildNodes()) { 
     161      NodeList nodes = el.getChildNodes(); 
     162      for(int i = 0;i<nodes.getLength();i++) { 
     163        Node thisNode = nodes.item(i); 
     164        if(thisNode instanceof Element) { 
     165          Element origChild = (Element) thisNode; 
     166          Element cloneChild = createClone(origChild,doc); 
     167          clone.appendChild(cloneChild); 
     168        } 
     169      } 
     170    } 
     171     
     172    return clone; 
    83173  } 
    84174   
Note: See TracChangeset for help on using the changeset viewer.