Coverage Report - org.kuali.rice.kns.util.documentserializer.PropertySerializerTrie
 
Classes in this File Line Coverage Branch Coverage Complexity
PropertySerializerTrie
0%
0/47
0%
0/24
4.4
 
 1  
 /*
 2  
  * Copyright 2007-2008 The Kuali Foundation
 3  
  *
 4  
  * Licensed under the Educational Community License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  * http://www.opensource.org/licenses/ecl2.php
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 package org.kuali.rice.kns.util.documentserializer;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.List;
 20  
 import java.util.StringTokenizer;
 21  
 
 22  
 import org.apache.commons.lang.StringUtils;
 23  
 import org.kuali.rice.kns.util.KNSConstants;
 24  
 
 25  
 /**
 26  
  * This is a implementation of a trie/prefix tree of that contains metadata about property serializability
 27  
  * during the document serialization process.
 28  
  *
 29  
  */
 30  
 public class PropertySerializerTrie {  
 31  
     private static final String PROPERTY_NAME_COMPONENT_SEPARATOR = ".";
 32  
     private PropertySerializerTrieNode rootNode;
 33  
     
 34  0
     public PropertySerializerTrie() {
 35  0
         rootNode = new PropertySerializerTrieNode(KNSConstants.EMPTY_STRING, KNSConstants.EMPTY_STRING);
 36  0
     }
 37  
     
 38  
     /**
 39  
      * Registers a new serializable property so that all of its primitives are serialized.  All nesting properties
 40  
      * will be serialized only to render open/close tags to maintain consistency with the document structure, unless
 41  
      * they are registered as well.
 42  
      * 
 43  
      * For example, if only property "document.a.b" is registered, then the XML will look like the following:
 44  
      * 
 45  
      * <document>
 46  
      *     <a>
 47  
      *         <b>
 48  
      *             <primitiveOfB>valueOfPrimitive</primitiveOfB>
 49  
      *         </b>
 50  
      *     </a>
 51  
      * </document>
 52  
      * 
 53  
      * That is, primitives of "document" and "document.a" will not be serialized unless those property strings are registered.
 54  
      * 
 55  
      * @param propertyName
 56  
      * @param setPropertySerializabilityToObjectAndAllPrimitivesForAll
 57  
      */
 58  
     public void addSerializablePropertyName(String propertyName, boolean setPropertySerializabilityToObjectAndAllPrimitivesForAll) {
 59  0
         if (propertyName == null) {
 60  0
             throw new IllegalArgumentException("Null attribute name specified");
 61  
         }
 62  0
         if (StringUtils.isBlank(propertyName)) {
 63  0
             rootNode.setPropertySerializabilityToObjectAndAllPrimitives();
 64  
         }
 65  
         else {
 66  0
             StringTokenizer tok = new StringTokenizer(propertyName, PROPERTY_NAME_COMPONENT_SEPARATOR, false);
 67  0
             StringBuilder buf = new StringBuilder();
 68  
 
 69  0
             if(setPropertySerializabilityToObjectAndAllPrimitivesForAll)
 70  0
                     rootNode.setPropertySerializabilityToObjectAndAllPrimitives();
 71  
 
 72  0
             PropertySerializerTrieNode currentNode = rootNode;
 73  0
             while (tok.hasMoreTokens()) {
 74  0
                 String attributeNameComponent = tok.nextToken();
 75  0
                 validateAttributeNameComponent(attributeNameComponent);
 76  
 
 77  0
                 buf.append(attributeNameComponent);
 78  
 
 79  
                 // create a new node or retrieve existing node for this name component
 80  0
                 PropertySerializerTrieNode childNode = currentNode.getChildNode(attributeNameComponent);
 81  0
                 if (childNode == null) {
 82  0
                     childNode = new PropertySerializerTrieNode(buf.toString(), attributeNameComponent);
 83  0
                     currentNode.addChildNode(childNode);
 84  
                 }
 85  
                 
 86  0
                 if (tok.hasMoreTokens()) {
 87  0
                     buf.append(PROPERTY_NAME_COMPONENT_SEPARATOR);
 88  
                 }
 89  0
                 currentNode = childNode;
 90  0
                 if(setPropertySerializabilityToObjectAndAllPrimitivesForAll)
 91  0
                         currentNode.setPropertySerializabilityToObjectAndAllPrimitives();
 92  0
             }
 93  
             
 94  0
             currentNode.setPropertySerializabilityToObjectAndAllPrimitives();
 95  
         }
 96  0
     }
 97  
     
 98  
     /**
 99  
      * Retrieves the metadata about the given property name
 100  
      * 
 101  
      * @param propertyName
 102  
      * @return
 103  
      */
 104  
     public PropertySerializabilityMetadata getPropertySerializabilityMetadata(String propertyName) {
 105  0
         if (propertyName == null) {
 106  0
             throw new IllegalArgumentException("Null attribute name specified");
 107  
         }
 108  0
         if (StringUtils.isBlank(propertyName)) {
 109  0
             return rootNode;
 110  
         }
 111  
         else {
 112  0
             StringTokenizer tok = new StringTokenizer(propertyName, PROPERTY_NAME_COMPONENT_SEPARATOR, false);
 113  
             
 114  0
             PropertySerializerTrieNode currentNode = rootNode;
 115  0
             while (tok.hasMoreTokens()) {
 116  0
                 String attributeNameComponent = tok.nextToken();
 117  0
                 validateAttributeNameComponent(attributeNameComponent);
 118  
 
 119  
                 // retrieve the child node for this name component
 120  0
                 PropertySerializerTrieNode childNode = currentNode.getChildNode(attributeNameComponent);
 121  0
                 if (childNode == null) {
 122  
                     // we didn't find a child node, so we know that something wasn't added with the prefix we're processing
 123  0
                     return null;
 124  
                 }
 125  
                 else {
 126  
                     // keep going until we hit the last token, at which case we'll get out of this loop
 127  0
                     currentNode = childNode;
 128  
                 }
 129  0
             }
 130  0
             return currentNode;
 131  
         }
 132  
     }
 133  
     
 134  
     /**
 135  
      * Returns the root node of the trie
 136  
      * 
 137  
      * @return
 138  
      */
 139  
     public PropertySerializabilityMetadata getRootPropertySerializibilityMetadata() {
 140  0
         return rootNode;
 141  
     }
 142  
     
 143  
     protected void validateAttributeNameComponent(String attributeNameComponent) {
 144  0
         if (StringUtils.isBlank(attributeNameComponent)) {
 145  0
             throw new IllegalArgumentException("Blank attribute name component specified");
 146  
         }
 147  0
     }
 148  
 }