Coverage Report - org.apache.johnzon.jaxrs.xml.WadlDocumentToJson
 
Classes in this File Line Coverage Branch Coverage Complexity
WadlDocumentToJson
97 %
39/40
90 %
20/22
5
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.johnzon.jaxrs.xml;
 20  
 
 21  
 import org.w3c.dom.Document;
 22  
 import org.w3c.dom.NamedNodeMap;
 23  
 import org.w3c.dom.Node;
 24  
 import org.w3c.dom.NodeList;
 25  
 
 26  
 import javax.json.Json;
 27  
 import javax.json.JsonArrayBuilder;
 28  
 import javax.json.JsonBuilderFactory;
 29  
 import javax.json.JsonObjectBuilder;
 30  
 import javax.xml.stream.XMLStreamException;
 31  
 import java.util.Collection;
 32  
 import java.util.Collections;
 33  
 import java.util.LinkedHashMap;
 34  
 import java.util.LinkedList;
 35  
 import java.util.Map;
 36  
 
 37  1
 public class WadlDocumentToJson {
 38  1
     private final JsonBuilderFactory builderFactory = Json.createBuilderFactory(Collections.<String, Object>emptyMap());
 39  
 
 40  
     public String convert(final Document doc) throws XMLStreamException {
 41  1
         final JsonObjectBuilder builder = builderFactory.createObjectBuilder();
 42  1
         if (doc.getChildNodes().getLength() != 1) {
 43  0
             return "{}";
 44  
         }
 45  1
         final Node item = doc.getChildNodes().item(0);
 46  1
         return builder.add(item.getNodeName(), createNode(item)).build().toString();
 47  
     }
 48  
 
 49  
     private void addChildrens(/*final String nodeName, */final JsonObjectBuilder builder, final NodeList children) {
 50  8
         final Map<String, Collection<Node>> nodesByName = new LinkedHashMap<String, Collection<Node>>();
 51  44
         for (int i = 0; i < children.getLength(); i++) {
 52  36
             final Node node = children.item(i);
 53  36
             if ("#text".equals(node.getNodeName())) {
 54  22
                 continue;
 55  
             }
 56  
 
 57  14
             final String name = node.getNodeName();
 58  14
             Collection<Node> nodes = nodesByName.get(name);
 59  14
             if (nodes == null) {
 60  11
                 nodes = new LinkedList<Node>();
 61  11
                 nodesByName.put(name, nodes);
 62  
             }
 63  14
             nodes.add(node);
 64  
         }
 65  
 
 66  8
         for (final Map.Entry<String, Collection<Node>> entry : nodesByName.entrySet()) {
 67  11
             final JsonArrayBuilder arrayBuilder = builderFactory.createArrayBuilder();
 68  11
             for (final Node n : entry.getValue()) {
 69  14
                 final JsonObjectBuilder jsonObjectBuilder = createNode(n);
 70  14
                 if (jsonObjectBuilder != null) {
 71  14
                     arrayBuilder.add(jsonObjectBuilder);
 72  
                 }
 73  14
             }
 74  11
             builder.add(entry.getKey(), arrayBuilder);
 75  11
         }
 76  8
     }
 77  
 
 78  
     private JsonObjectBuilder createNode(final Node node) {
 79  15
         JsonObjectBuilder childBuilder = null;
 80  
 
 81  15
         if (node.hasAttributes()) {
 82  14
             childBuilder = builderFactory.createObjectBuilder();
 83  14
             final NamedNodeMap attributes = node.getAttributes();
 84  36
             for (int j = 0; j < attributes.getLength(); j++) {
 85  22
                 final Node namedItem = attributes.item(j);
 86  22
                 childBuilder.add(namedItem.getNodeName(), namedItem.getNodeValue());
 87  
             }
 88  
         }
 89  
 
 90  15
         if (node.hasChildNodes()) {
 91  8
             if (childBuilder == null) {
 92  1
                 childBuilder = builderFactory.createObjectBuilder();
 93  
             }
 94  8
             addChildrens(/*node.getNodeName(),*/ childBuilder, node.getChildNodes());
 95  
         }
 96  15
         return childBuilder;
 97  
     }
 98  
 }