src/main/java/eu/digitisation/layout/SortPageXML.java
private static void sort(Node node, List<String> order) {
        Map<String, Node> index = new HashMap<String, Node>();  // index of children nodes
        NodeList children = node.getChildNodes();
        List<Node> childList = new ArrayList<Node>();   // External copy of children

        // Initialize index (only nodes which need reordering will be stored)
        for (String id : order) {
            index.put(id, null);
        }

        // Create an index ot text regions which need reordering
        for (int n = 0; n < children.getLength(); ++n) {
            Node child = children.item(n);
            childList.add(child);
            if (child instanceof Element) {
                String id = ((Element) child).getAttribute("id");
                if (index.containsKey(id)) {
                    index.put(id, child);
                }
            }
        }

        // Clear internal list of child nodes
        while (children.getLength() > 0) {
            node.removeChild(children.item(0));
        }

        int norder = 0; // the posititon in the order list
        for (int n = 0; n < childList.size(); ++n) {
            Node child = childList.get(n);
            if (child instanceof Element) {
                String id = ((Element) child).getAttribute("id");

                if (index.containsKey(id)) {
                    Node replacement = index.get(order.get(norder));
                    node.appendChild(replacement);
                    ++norder;
                } else {
                    node.appendChild(child);
                }
            } else {
                node.appendChild(child);
            }
        }
    }
/**
     * SortPageXML children consistently with the order defined for their id
     * attribute Remark: NodeList mutates when operations on nodes take place:
     * an backup childList is used to avoid such conflicts.
     *
     * @param node the parent node
     * @param order the array of id's in ascending order
     */
SortPageXML children consistently with the order defined for their id
attribute Remark: NodeList mutates when operations on nodes take place:
an backup childList is used to avoid such conflicts.

@param node the parent node
@param order the array of id's in ascending order
SortPageXML children consistently with the order defined for their id attribute Remark : NodeList mutates when operations on nodes take place : an backup childList is used to avoid such conflicts . @param node the parent node @param order the array of id ' s in ascending order
private static void sort(Node node, List<String> order) {
        Map<String, Node> index = new HashMap<String, Node>();  
        NodeList children = node.getChildNodes();
        List<Node> childList = new ArrayList<Node>();   
        for (String id : order) {
            index.put(id, null);
        }
        for (int n = 0; n < children.getLength(); ++n) {
            Node child = children.item(n);
            childList.add(child);
            if (child instanceof Element) {
                String id = ((Element) child).getAttribute("id");
                if (index.containsKey(id)) {
                    index.put(id, child);
                }
            }
        }
        while (children.getLength() > 0) {
            node.removeChild(children.item(0));
        }
        int norder = 0; 
        for (int n = 0; n < childList.size(); ++n) {
            Node child = childList.get(n);
            if (child instanceof Element) {
                String id = ((Element) child).getAttribute("id");
                if (index.containsKey(id)) {
                    Node replacement = index.get(order.get(norder));
                    node.appendChild(replacement);
                    ++norder;
                } else {
                    node.appendChild(child);
                }
            } else {
                node.appendChild(child);
            }
        }
    }
private static void sort ( Node node , List < String > order ) { Map < String , Node > index = new HashMap < String , Node > ( ) ; NodeList children = node . getChildNodes ( ) ; List < Node > childList = new ArrayList < Node > ( ) ; for ( String id : order ) { index . put ( id , null ) ; } for ( int n = 0 ; n < children . getLength ( ) ; ++ n ) { Node child = children . item ( n ) ; childList . add ( child ) ; if ( child instanceof Element ) { String id = ( ( Element ) child ) . getAttribute ( "id" ) ; if ( index . containsKey ( id ) ) { index . put ( id , child ) ; } } } while ( children . getLength ( ) > 0 ) { node . removeChild ( children . item ( 0 ) ) ; } int norder = 0 ; for ( int n = 0 ; n < childList . size ( ) ; ++ n ) { Node child = childList . get ( n ) ; if ( child instanceof Element ) { String id = ( ( Element ) child ) . getAttribute ( "id" ) ; if ( index . containsKey ( id ) ) { Node replacement = index . get ( order . get ( norder ) ) ; node . appendChild ( replacement ) ; ++ norder ; } else { node . appendChild ( child ) ; } } else { node . appendChild ( child ) ; } } }
SortPageXML children consistently with the order defined for their id
 attribute Remark: NodeList mutates when operations on nodes take place:
 an backup childList is used to avoid such conflicts.
SortPageXML children consistently with the order defined for their id attribute Remark : NodeList mutates when operations on nodes take place : an backup childList is used to avoid such conflicts .
// index of children nodes // External copy of children // Initialize index (only nodes which need reordering will be stored) // Create an index ot text regions which need reordering // Clear internal list of child nodes // the posititon in the order list