001package jmri.jmrit.signalsystemeditor.configurexml;
002
003import java.util.List;
004
005import org.jdom2.Content;
006import org.jdom2.Element;
007
008import jmri.jmrit.signalsystemeditor.StringWithLinks;
009
010/**
011 * Load and store StringWithLinks
012 *
013 * @author Daniel Bergqvist (C) 2022
014 */
015public class StringWithLinksXml {
016
017    public static StringWithLinks load(Element element) {
018        StringWithLinks swl = new StringWithLinks();
019        List<String> _strings = swl.getStrings();
020        List<StringWithLinks.Link> _links = swl.getLinks();
021        for (Content content : element.getContent()) {
022            if (content.getCType() == Content.CType.Text) {
023                int stringsSize = _strings.size();
024                if (stringsSize > _links.size()) {
025                    _strings.set(stringsSize-1, _strings.get(stringsSize-1)+content.getValue());
026                } else {
027                    _strings.add(content.getValue());
028                }
029            } else if (content.getCType() == Content.CType.Element) {
030                Element e = (Element) content;
031                if ("a".equals(e.getName())) {
032                    if (_strings.size() <= _links.size()) {
033                        _strings.add("");
034                    }
035                    _links.add(new StringWithLinks.Link(e.getText(), e.getAttributeValue("href")));
036                } else {
037                    throw new RuntimeException("Unkown tag: " + e.getName());
038                }
039            } else {
040                throw new RuntimeException("Unkown CType: " + content.getCType().name());
041            }
042        }
043
044        // If no links, just read the text
045        if (_links.isEmpty()) {
046            _strings.clear();
047            _strings.add(element.getText());
048        }
049        return swl;
050    }
051
052    public static Element store(StringWithLinks stringWithLinks, String tagName) {
053
054        Element element = new Element(tagName);
055
056        List<String> _strings = stringWithLinks.getStrings();
057        List<StringWithLinks.Link> _links = stringWithLinks.getLinks();
058
059        if (_strings.size() == 1 && _links.isEmpty()) {
060            element.setText(_strings.get(0));
061            return element;
062        } else {
063            int i=0;
064            while (i < _strings.size() || i < _links.size()) {
065                if (i < _strings.size()) {
066                    StringBuilder sb = new StringBuilder(_strings.get(i));
067                    // Replace spaces with non breaking spaces at the beginning of the string
068                    for (int j=0; j < sb.length(); j++) {
069                        if (sb.charAt(j) == ' ') {
070                            sb.setCharAt(j, '\u00A0');
071                        } else {
072                            break;
073                        }
074                    }
075                    // Replace spaces with non breaking spaces at the end of the string
076                    for (int j=sb.length()-1; j > 0; j--) {
077                        if (sb.charAt(j) == ' ') {
078                            sb.setCharAt(j, '\u00A0');
079                        } else {
080                            break;
081                        }
082                    }
083                    element.addContent(sb.toString());
084                }
085                if (i < _links.size()) {
086                    Element link = new Element("a");
087                    link.setText(_links.get(i).getName());
088                    link.setAttribute("href", _links.get(i).getHref());
089                    element.addContent(link);
090                }
091                i++;
092            }
093            return !element.getChildren().isEmpty() ? element : null;
094        }
095    }
096
097}