Java tutorial
/* * Copyright (C) 2006-2016 Talend Inc. - www.talend.com * * This source code is available under agreement available at * %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt * * You should have received a copy of the agreement * along with this program; if not, write to Talend SA * 9 rue Pages 92150 Suresnes, France */ package com.amalto.core.history; import org.apache.commons.lang.StringUtils; import org.w3c.dom.*; import java.util.Stack; /** * A {@link com.amalto.core.history.DocumentTransformer} that marks the {@link com.amalto.core.history.Document} elements with unique identifiers that depends on their * position in the document. * For instance a document such as: * <p> * <code> * <root><br/> * <field1 cls="tree-node-different"><br/> * <field>newValue1</field><br/> * </field1><br/> * <field2 cls="tree-node-different"><br/> * <field>newValue2</field><br/> * </field2><br/> * <field3 cls="tree-node-different"><br/> * <field>newValue3</field><br/> * </field3><br/> * </root><br/> * </code> * </p> * Would be transformed into: * <p> * <code> * <root id="root-1"><br/> * <field1 cls="tree-node-different" id="1-field1-1"><br/> * <field id="11-field-1">newValue1</field><br/> * </field1><br/> * <field2 cls="tree-node-different" id="1-field2-2"><br/> * <field id="12-field-1">newValue2</field><br/> * </field2><br/> * <field3 cls="tree-node-different" id="1-field3-3"><br/> * <field id="13-field-1">newValue3</field><br/> * </field3><br/> * </root><br/> * </code> * </p> */ public class UniqueIdTransformer implements DocumentTransformer { private static final String ID_ATTRIBUTE_NAME = "id"; //$NON-NLS-1$ public UniqueIdTransformer() { } public Document transform(MutableDocument document) { org.w3c.dom.Document domDocument = document.asDOM(); addIds(domDocument); return document; } public void addIds(org.w3c.dom.Document document) { Stack<Integer> levels = new Stack<Integer>(); levels.push(0); { Element documentElement = document.getDocumentElement(); if (documentElement != null) { _addIds(document, documentElement, levels); } } levels.pop(); } private void _addIds(org.w3c.dom.Document document, Element element, Stack<Integer> levels) { NamedNodeMap attributes = element.getAttributes(); Attr id = document.createAttribute(ID_ATTRIBUTE_NAME); int thisElementId = levels.pop() + 1; StringBuilder builder; { builder = new StringBuilder(); for (Integer level : levels) { builder.append(level); } } String prefix = builder.toString().isEmpty() ? StringUtils.EMPTY : builder.toString() + '-'; id.setValue(prefix + element.getNodeName() + '-' + thisElementId); attributes.setNamedItem(id); levels.push(thisElementId); { levels.push(0); NodeList children = element.getChildNodes(); for (int i = 0; i < children.getLength(); i++) { if (children.item(i).getNodeType() == Node.ELEMENT_NODE) { Element child = (Element) children.item(i); _addIds(document, child, levels); } } levels.pop(); } } }