class NodeWriter {

    public void writeToNode(Node node, List<WMSLayerAndState> layerStates, boolean saveDataLayers)
    {
        List<WMSLayerState> dataLayers = New.list();
        List<WMSLayerState> mapLayers = New.list();

        segregateLayers(layerStates, dataLayers, mapLayers);

        Document doc = node instanceof Document ? (Document)node : node.getOwnerDocument();

        try
        {
            if (saveDataLayers)
            {
                if (!dataLayers.isEmpty())
                {
                    Element dataLayerNode = (Element)StateXML.createChildNode(node, doc, node, StateConstants.DATA_LAYERS_PATH,
                            StateConstants.LAYERS_NAME);
                    dataLayerNode.setAttribute("type", StateConstants.DATA_LAYERS_TYPE);
                    addToNode(dataLayerNode, dataLayers);
                }
            }
            else if (!mapLayers.isEmpty())
            {
                Element mapLayerNode = (Element)StateXML.createChildNode(node, doc, node, StateConstants.MAP_LAYERS_PATH,
                        StateConstants.LAYERS_NAME);
                mapLayerNode.setAttribute("type", StateConstants.MAP_LAYERS_TYPE);
                addToNode(mapLayerNode, mapLayers);
            }
        }
        catch (XPathExpressionException e)
        {
            LOGGER.error(e.getMessage(), e);
        }
    }

}