/*
 * Copyright (C) 2014 B3Partners B.V.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package nl.b3p.viewer.print;

import java.io.ByteArrayInputStream;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlType;
import javax.xml.parsers.DocumentBuilderFactory;
import org.json.JSONObject;
import org.w3c.dom.Node;

/**
 *
 * @author Meine Toonen
 */
@XmlType//(propOrder = {"className","componentName","info"})
public class PrintExtraInfo {
    
    private String className;
    private String componentName;
    
    private Node info;

    
    @XmlAttribute(name="classname")
    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }
    
    @XmlAttribute(name ="componentname")
    public String getComponentName() {
        return componentName;
    }

    public void setComponentName(String componentName) {
        this.componentName = componentName;
    }
   
    @XmlAnyElement
    public Node getInfo() {
        return info;
    }

    public void setInfoText(JSONObject j) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        JSONObject root = new JSONObject();
        root.put("root", j);
        String s = org.json.XML.toString( root);
        this.info =dbf.newDocumentBuilder().parse(new ByteArrayInputStream(s.getBytes("UTF-8"))).getDocumentElement();
        
    }
    
}

@@ -775,16 +775,80 @@
 Stream;%0A
+import java.util.regex.Matcher;%0Aimport java.util.regex.Pattern;%0A
 import j
@@ -2065,15 +2065,645 @@
 ing(
- 
 root);
+%0A        // replace spaces in element names, because the json lib we use is not%0A        // smart enough and the produces string will have spaces in element names%0A        // is attribute nems have spaces%0A        // see: https://stackoverflow.com/questions/19095106/regular-expression-replace-whitespaces-inside-tag%0A        final Pattern p = Pattern.compile(%22(?s)(?%3C=%3C).*?(?=/?%3E%7C%5C%5Cs*%5C%5Cw+%5C%5Cs*=)%22);%0A        Matcher m = p.matcher(s);%0A        StringBuffer sb = new StringBuffer();%0A        while (m.find()) %7B%0A            m.appendReplacement(sb, m.group().replace(%22 %22, %22_%22));%0A        %7D%0A        m.appendTail(sb);%0A        s = sb.toString();%0A
 %0A   

add a fix for the removed space -> underscore replacement at the most appropriate point which is when serializing json to xml