Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package de.swm.gwt.linker;
  
  import java.util.HashMap;
  import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 

Author(s):
Daniel Kurka (see http://code.google.com/p/mgwt/)
 
 public class XMLPermutationProvider implements Serializable {
 	
         private static final long serialVersionUID = -8892369911664489332L;
         private static final String PERMUTATION_NODE = "permutation";
         private static final String PERMUTATION_NAME = "name";
         private static final String PERMUTATIONS = "permutations";
 
         protected static final Logger logger = Logger.getLogger(XMLPermutationProvider.class.getName());
 
 
                 Map<StringList<BindingProperty>> map = new HashMap<StringList<BindingProperty>>();
 
                 Document document = createDocumentFromInputStream(stream);
 
                 Element permutationsNode = document.getDocumentElement();
 
                 String tagName = permutationsNode.getTagName();
                 if (!.equals(tagName)) {
                         .severe("unexpected xml structure: Expected node : '" +  + "' got: '" + tagName + "'");
                         throw new XMLPermutationProviderException();
                 }
 
                 NodeList permutationsChildren = permutationsNode.getChildNodes();
 
                 for (int i = 0; i < permutationsChildren.getLength(); i++) {
                         Node node = permutationsChildren.item(i);
 
                         if (node.getNodeType() != .) {
                                 continue;
                         }
                         Element permutationNode = (Elementnode;
                         handlePermutation(mappermutationNode);
                 }
 
                 return map;
 
         }
 
         protected void handlePermutation(Map<StringList<BindingProperty>> mapElement permutationNodethrows XMLPermutationProviderException {
 
                 String strongName = permutationNode.getAttribute();
 
                 ArrayList<BindingPropertylist = new ArrayList<BindingProperty>();
                 map.put(strongNamelist);
 
                 NodeList variableNodes = permutationNode.getChildNodes();
                 for (int i = 0; i < variableNodes.getLength(); i++) {
                         Node item = variableNodes.item(i);
                         if (item.getNodeType() != .)
                                 continue;
                         Element variables = (Elementitem;
                         String varKey = variables.getTagName();
                         NodeList childNodes = variables.getChildNodes();
                         if (childNodes.getLength() != 1) {
                                 .severe("Unexpected XML Structure: Expected property value");
                                 throw new XMLPermutationProviderException();
                         }
                        String varValue = childNodes.item(0).getNodeValue();
                        BindingProperty bindingProperty = new BindingProperty(varKeyvarValue);
                        list.add(bindingProperty);
                }
        }
        public String serializeMap(Map<StringSet<BindingProperty>> mapthrows XMLPermutationProviderException {
                Document document = createDocument();
                Element permutationsNode = document.createElement();
                document.appendChild(permutationsNode);
                for (Entry<StringSet<BindingProperty>> entry : map.entrySet()) {
                        Element node = document.createElement();
                        node.setAttribute(entry.getKey());
                        permutationsNode.appendChild(node);
                        for (BindingProperty b : entry.getValue()) {
                                Element variable = document.createElement(b.getName());
                                variable.appendChild(document.createTextNode(b.getValue()));
                                node.appendChild(variable);
                        }
                }
                return transformDocumentToString(document);
        }
        protected Document createDocument() throws XMLPermutationProviderException {
                try {
                        return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
                } catch (ParserConfigurationException e) {
                        .log(."can not create new document"e);
                        throw new XMLPermutationProviderException("can not create new document"e);
                }
        }
        protected String transformDocumentToString(Document documentthrows XMLPermutationProviderException {
                try {
                        StringWriter xml = new StringWriter();
                        Transformer transformer = TransformerFactory.newInstance().newTransformer();
                        transformer.setOutputProperty(."yes");
                        transformer.transform(new DOMSource(document), new StreamResult(xml));
                        String permMapString = xml.toString();
                        return permMapString;
                } catch (TransformerConfigurationException e) {
                        .log(."can not transform document to String");
                        throw new XMLPermutationProviderException("can not transform document to String"e);
                } catch (TransformerFactoryConfigurationError e) {
                        .log(."can not transform document to String");
                        throw new XMLPermutationProviderException("can not transform document to String"e);
                } catch (TransformerException e) {
                        .log(."can not transform document to String");
                        throw new XMLPermutationProviderException("can not transform document to String"e);
                }
        }
        public String writePermutationInformation(String strongNameSet<BindingPropertybindingPropertiesSet<Stringfilesthrows XMLPermutationProviderException {
                Document document = createDocument();
                Element permutationNode = document.createElement();
                document.appendChild(permutationNode);
                permutationNode.setAttribute(strongName);
                // create and append variables node
                Element variablesNode = document.createElement("variables");
                permutationNode.appendChild(variablesNode);
                // write out all variables
                for (BindingProperty prop : bindingProperties) {
                        Element varNode = document.createElement(prop.getName());
                        varNode.appendChild(document.createTextNode(prop.getValue()));
                        variablesNode.appendChild(varNode);
                }
                // create file node
                Element filesNode = document.createElement("files");
                permutationNode.appendChild(filesNode);
                // write out all files
                for (String string : files) {
                        Element fileNode = document.createElement("file");
                        fileNode.appendChild(document.createTextNode(string));
                        filesNode.appendChild(fileNode);
                }
                return transformDocumentToString(document);
        }
        public Set<StringgetPermutationFiles(InputStream inputStreamthrows XMLPermutationProviderException {
                Document document = createDocumentFromInputStream(inputStream);
                Element documentNode = document.getDocumentElement();
                Set<Stringset = new HashSet<String>();
                NodeList mainNodes = documentNode.getChildNodes();
                for (int i = 0; i < mainNodes.getLength(); i++) {
                        Node item = mainNodes.item(i);
                        if (item.getNodeType() != .)
                                continue;
                        Element variables = (Elementitem;
                        String varKey = variables.getTagName();
                        if ("files".equals(varKey)) {
                                NodeList fileNodes = variables.getChildNodes();
                                handleFileNodes(setfileNodes);
                        }
                }
                return set;
        }
        protected Document createDocumentFromInputStream(InputStream inputStreamthrows XMLPermutationProviderException {
                try {
                        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                        return builder.parse(inputStream);
                } catch (SAXException e) {
                        .log(."can not parse input stream"e);
                        throw new XMLPermutationProviderException("can not parse input stream"e);
                } catch (IOException e) {
                        .log(."can not parse input stream"e);
                        throw new XMLPermutationProviderException("can not parse input stream"e);
                } catch (ParserConfigurationException e) {
                        .log(."can not parse input stream"e);
                        throw new XMLPermutationProviderException("can not parse input stream"e);
                }
        }
        protected void handleFileNodes(Set<StringsetNodeList fileNodesthrows XMLPermutationProviderException {
                for (int i = 0; i < fileNodes.getLength(); i++) {
                        Node item = fileNodes.item(i);
                        if (item.getNodeType() != .)
                                continue;
                        Element fileNode = (Elementitem;
                        NodeList childNodes = fileNode.getChildNodes();
                        if (childNodes.getLength() != 1) {
                                .severe("Unexpected XML Structure: Expected property value");
                                throw new XMLPermutationProviderException();
                        }
                        String varValue = childNodes.item(0).getNodeValue();
                        set.add(varValue);
                }
        }
New to GrepCode? Check out our FAQ X