Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.espertech.esperio.socket.config;
  
  import org.w3c.dom.Node;
 
 
 {
    
Use the configuration specified in the given input stream.

Parameters:
configuration is the configuration object to populate
stream Inputstream to be read from
resourceName The name to use in warning/error messages
Throws:
java.lang.RuntimeException is thrown when the configuration could not be parsed
 
     protected static void doConfigure(ConfigurationSocketAdapter configurationInputStream streamString resourceNamethrows RuntimeException
     {
         Document document = getDocument(streamresourceName);
         doConfigure(configurationdocument);
     }

    
Returns the document.

Parameters:
stream to read
resourceName resource in stream
Returns:
document
Throws:
java.lang.RuntimeException if the document could not be loaded or parsed
 
     protected static Document getDocument(InputStream streamString resourceNamethrows RuntimeException
     {
         DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
         DocumentBuilder builder;
 
         Document document = null;
 
         try
         {
             builder = factory.newDocumentBuilder();
             document = builder.parse(stream);
         }
         catch (ParserConfigurationException ex)
         {
             throw new RuntimeException("Could not get a DOM parser configuration: " + resourceNameex);
         }
         catch (SAXException ex)
         {
             throw new RuntimeException("Could not parse configuration: " + resourceNameex);
         }
         catch (IOException ex)
         {
             throw new RuntimeException("Could not read configuration: " + resourceNameex);
         }
         finally {
             try {
                 stream.close();
             }
             catch (IOException ioe) {
                 .warn"could not close input stream for: " + resourceNameioe );
             }
         }
 
         return document;
     }

    
Parse the W3C DOM document.

Parameters:
configuration is the configuration object to populate
doc to parse
Throws:
java.lang.RuntimeException to indicate parse errors
 
     protected static void doConfigure(ConfigurationSocketAdapter configurationDocument docthrows RuntimeException
     {
         Element root = doc.getDocumentElement();
 
         DOMElementIterator eventTypeNodeIterator = new DOMElementIterator(root.getChildNodes());
         while (eventTypeNodeIterator.hasNext())
         {
             Element element = eventTypeNodeIterator.next();
             String nodeName = element.getNodeName();
             if (nodeName.equals("socket"))
             {
                 handleSocket(configurationelement);
             }
         }
     }
 
     private static void handleSocket(ConfigurationSocketAdapter configurationNode node)
     {
        String name = getRequiredAttribute(node"name");
        String port = getRequiredAttribute(node"port");
        String dataType = getRequiredAttribute(node"data");
        String hostname = getOptionalAttribute(node"hostname");
        String backlog = getOptionalAttribute(node"backlog");
        String stream = getOptionalAttribute(node"stream");
        String propertyOrder = getOptionalAttribute(node"propertyOrder");
        String unescapeStr = getOptionalAttribute(node"unescape");
        SocketConfig socketConfig = new SocketConfig();
        socketConfig.setPort(Integer.parseInt(port));
        socketConfig.setDataType(DataType.valueOf(dataType.toUpperCase()));
        socketConfig.setHostname(hostname);
        socketConfig.setStream(stream);
        socketConfig.setPropertyOrder(propertyOrder);
        if (backlog != null) {
            socketConfig.setBacklog(Integer.parseInt(backlog));
        }
        if (unescapeStr != null) {
            socketConfig.setUnescape(Boolean.parseBoolean(unescapeStr));
        }
        configuration.getSockets().put(namesocketConfig);
    }

    
Returns an input stream from an application resource in the classpath.

Parameters:
resource to get input stream for
Returns:
input stream for resource
    protected static InputStream getResourceAsStream(String resource)
    {
        String stripped = resource.startsWith("/") ?
                resource.substring(1) : resource;
        InputStream stream = null;
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        if (classLoader!=null) {
            stream = classLoader.getResourceAsStreamstripped );
        }
        if ( stream == null ) {
            ConfigurationSocketAdapterParser.class.getResourceAsStreamresource );
        }
        if ( stream == null ) {
            stream = ConfigurationSocketAdapterParser.class.getClassLoader().getResourceAsStreamstripped );
        }
        if ( stream == null ) {
            throw new RuntimeExceptionresource + " not found" );
        }
        return stream;
    }
    private static String getOptionalAttribute(Node nodeString key)
    {
        Node valueNode = node.getAttributes().getNamedItem(key);
        if (valueNode != null)
        {
            return valueNode.getTextContent();
        }
        return null;
    }
    private static String getRequiredAttribute(Node nodeString key)
    {
        Node valueNode = node.getAttributes().getNamedItem(key);
        if (valueNode == null)
        {
            throw new ConfigurationException("Required attribute by name '" + key + "' not found");
        }
        return valueNode.getTextContent();
    }
    private static Log log = LogFactory.getLog(ConfigurationSocketAdapterParser.class);
New to GrepCode? Check out our FAQ X