juddi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From svi...@apache.org
Subject cvs commit: ws-juddi/src/java/org/apache/juddi/util/xml XMLUtils.java
Date Thu, 10 Feb 2005 03:29:05 GMT
sviens      2005/02/09 19:29:05

  Modified:    src/junit/org/apache/juddi/handler HandlerTestCase.java
               src/java/org/apache/juddi/registry RegistryServlet.java
               src/java/org/apache/juddi/util/xml XMLUtils.java
  Log:
  Removed dependencies on JDOM
  
  Revision  Changes    Path
  1.2       +14 -25    ws-juddi/src/junit/org/apache/juddi/handler/HandlerTestCase.java
  
  Index: HandlerTestCase.java
  ===================================================================
  RCS file: /home/cvs/ws-juddi/src/junit/org/apache/juddi/handler/HandlerTestCase.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- HandlerTestCase.java	16 Jan 2005 03:51:27 -0000	1.1
  +++ HandlerTestCase.java	10 Feb 2005 03:29:05 -0000	1.2
  @@ -1,24 +1,27 @@
   /*
  - * Created on Jan 15, 2005
  + * Copyright 2001-2004 The Apache Software Foundation.
    *
  - * TODO To change the template for this generated file go to
  - * Window - Preferences - Java - Code Style - Code Templates
  + * Licensed under the Apache License, Version 2.0 (the "License");
  + * you may not use this file except in compliance with the License.
  + * You may obtain a copy of the License at
  + *
  + *      http://www.apache.org/licenses/LICENSE-2.0
  + *
  + * Unless required by applicable law or agreed to in writing, software
  + * distributed under the License is distributed on an "AS IS" BASIS,
  + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  + * See the License for the specific language governing permissions and
  + * limitations under the License.
    */
   package org.apache.juddi.handler;
   
  -import java.io.IOException;
  -import java.io.StringWriter;
  -
   import junit.framework.TestCase;
   
   import org.apache.juddi.util.xml.XMLUtils;
   import org.w3c.dom.Element;
   
   /**
  - * @author sviens
  - *
  - * TODO To change the template for this generated type comment go to
  - * Window - Preferences - Java - Code Style - Code Templates
  + * @author Steve Viens (sviens@apache.org)
    */
   public abstract class HandlerTestCase extends TestCase
   {
  @@ -29,20 +32,6 @@
   	
   	protected final String getXMLString(Element element)
   	{
  -		StringWriter writer = new StringWriter();
  -        
  -		XMLUtils.writeXML(element,writer);
  -
  -		String xmlString = writer.toString();
  -
  -		try
  -		{
  -			writer.close();
  -		}
  -		catch(IOException exp)
  -		{
  -		}
  -
  -		return xmlString;
  +        return XMLUtils.toString(element);
   	}
   }
  
  
  
  1.8       +51 -45    ws-juddi/src/java/org/apache/juddi/registry/RegistryServlet.java
  
  Index: RegistryServlet.java
  ===================================================================
  RCS file: /home/cvs/ws-juddi/src/java/org/apache/juddi/registry/RegistryServlet.java,v
  retrieving revision 1.7
  retrieving revision 1.8
  diff -u -r1.7 -r1.8
  --- RegistryServlet.java	9 Feb 2005 04:47:48 -0000	1.7
  +++ RegistryServlet.java	10 Feb 2005 03:29:05 -0000	1.8
  @@ -27,9 +27,9 @@
   import javax.xml.parsers.DocumentBuilder;
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.parsers.ParserConfigurationException;
  +import javax.xml.soap.Detail;
   import javax.xml.soap.MessageFactory;
   import javax.xml.soap.SOAPBody;
  -import javax.xml.soap.SOAPElement;
   import javax.xml.soap.SOAPEnvelope;
   import javax.xml.soap.SOAPException;
   import javax.xml.soap.SOAPFault;
  @@ -46,6 +46,7 @@
   import org.apache.juddi.handler.HandlerMaker;
   import org.apache.juddi.handler.IHandler;
   import org.apache.juddi.util.Config;
  +import org.apache.juddi.util.xml.XMLUtils;
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
   
  @@ -97,8 +98,8 @@
           propFile = DEFAULT_PROPERTY_FILE;
         
         InputStream is = 
  -      	getServletContext().getResourceAsStream(propFile);
  -    	
  +        getServletContext().getResourceAsStream(propFile);
  +        
         if (is != null)
         {
           log.info("Resources loaded from: "+propFile);
  @@ -112,11 +113,11 @@
         else
         {
           log.warn("Could not locate jUDDI properties '" + propFile + 
  -        		"'. Using defaults.");
  +                "'. Using defaults.");
   
           // A juddi.properties file doesn't exist
           // yet so create create a new Properties 
  -      	// instance using default property values.
  +        // instance using default property values.
           
           props.put(RegistryEngine.PROPNAME_OPERATOR_NAME,
                     RegistryEngine.DEFAULT_OPERATOR_NAME);
  @@ -206,6 +207,8 @@
     public void doPost(HttpServletRequest req, HttpServletResponse res)
       throws ServletException, IOException
     {
  +    res.setContentType("text/xml; charset=utf-8");
  +      
       SOAPMessage soapReq = null;
       SOAPMessage soapRes = null;
       String generic = null; // TODO (Steve) References to generic should be renamed to "UDDI
Version or UDDI Namespace"
  @@ -278,6 +281,7 @@
         // results to the body of the SOAP response.
           
         responseHandler.marshal(uddiResObj,element);
  +      log.debug(XMLUtils.toString((Element)element.getFirstChild()));
         
         // Grab a reference to the 'temp' element's
         // only child here (this has the effect of
  @@ -288,44 +292,48 @@
         soapRes.getSOAPBody().addDocument(document);
       } 
       catch (RegistryException ex) {             
  -      log.error(ex);
  -    
  -      try {
  -        SOAPEnvelope envelope = soapRes.getSOAPPart().getEnvelope();
  -        SOAPBody body = envelope.getBody(); 
  -        SOAPFault soapFault = body.addFault();
  -        soapFault.setFaultActor(ex.getFaultActor());
  -        soapFault.setFaultCode(ex.getFaultCode());
  -        soapFault.setFaultString(ex.getFaultString());
  -
  -        // Store DispositionReport in the SOAP fault node.
  -        DispositionReport dispRpt = ex.getDispositionReport();
  -        if (dispRpt != null)
  -        {
  -          dispRpt.setGeneric(generic);
  -          dispRpt.setOperator(Config.getOperator());
  -        }
  -                
  -        // Create a new 'temp' XML element to use as a container 
  -        // in which to marshal the DispositionReport into.
  -          
  -        DocumentBuilder docBuilder = getDocumentBuilder();
  -        Document document = docBuilder.newDocument();
  -        Element dispRptElement = document.createElement("temp");
  -
  -        // Lookup the DispositionReportHandler and marshal 
  -        // the juddi object into the appropriate xml format (we 
  -        // only support UDDI v2.0 at this time).  Attach the
  -        // results to the body of the SOAP fault.
  +        log.error(ex);
  +      
  +        try {        
  +          SOAPBody body = soapRes.getSOAPBody();
  +          SOAPFault soapFault = body.addFault();
  +          soapFault.setFaultActor((ex.getFaultActor() == null) ? "" : ex.getFaultActor());
  +          soapFault.setFaultString((ex.getFaultString() == null) ? "" : ex.getFaultString());
  +          soapFault.setFaultCode((ex.getFaultCode() == null) ? "" : ex.getFaultCode());
             
  -        IHandler dispRptHandler = maker.lookup(DispositionReport.class.getName());
  -        dispRptHandler.marshal(dispRpt,dispRptElement);        
  -        soapFault.addChildElement((SOAPElement)dispRptElement);
  -      }
  -      catch(SOAPException sex) {
  -        log.error(sex);
  -      }
  -    } 
  +          // Store DispositionReport in the SOAP fault node.
  +          DispositionReport dispRpt = ex.getDispositionReport();
  +          if (dispRpt != null)
  +          {          
  +            dispRpt.setGeneric(generic);
  +            dispRpt.setOperator(Config.getOperator());
  +
  +            // Create a new 'temp' XML element to use as a container 
  +            // in which to marshal the DispositionReport into.
  +              
  +            DocumentBuilder docBuilder = getDocumentBuilder();
  +            Document document = docBuilder.newDocument();
  +            Element dispRptElement = document.createElement("temp");
  +
  +            // Lookup the DispositionReportHandler and marshal 
  +            // the juddi object into the appropriate xml format (we 
  +            // only support UDDI v2.0 at this time).  Attach the
  +            // results to the body of the SOAP fault.
  +              
  +            IHandler dispRptHandler = maker.lookup(DispositionReport.class.getName());
  +            dispRptHandler.marshal(dispRpt,dispRptElement);   
  +            
  +            // Create the SOAPFault Detail element and insert 
  +            // the DispositionReport.
  +            
  +            Detail soapDetail = soapFault.addDetail();
  +            soapDetail.appendChild(dispRptElement.getFirstChild());
  +          }
  +        }
  +        catch(SOAPException sex) {
  +          log.error(sex);
  +        }
  +      } 
       catch (SOAPException ex) {             
         log.error(ex);
   
  @@ -354,8 +362,6 @@
       }
       finally {
         try {               
  -        res.setContentType("text/xml; charset=utf-8");
  -
           //soapRes.writeTo(System.out);     
           soapRes.writeTo(res.getOutputStream());     
         }
  @@ -415,4 +421,4 @@
   
       return docBuilder;
     }
  -}
  \ No newline at end of file
  +}
  
  
  
  1.10      +99 -39    ws-juddi/src/java/org/apache/juddi/util/xml/XMLUtils.java
  
  Index: XMLUtils.java
  ===================================================================
  RCS file: /home/cvs/ws-juddi/src/java/org/apache/juddi/util/xml/XMLUtils.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- XMLUtils.java	6 Feb 2005 17:27:12 -0000	1.9
  +++ XMLUtils.java	10 Feb 2005 03:29:05 -0000	1.10
  @@ -15,23 +15,28 @@
    */
   package org.apache.juddi.util.xml;
   
  -import java.io.IOException;
  +import java.io.ByteArrayOutputStream;
   import java.io.OutputStream;
  -import java.io.StringWriter;
  -import java.io.Writer;
  +import java.net.URL;
   import java.util.Vector;
   
   import javax.xml.parsers.DocumentBuilder;
   import javax.xml.parsers.DocumentBuilderFactory;
   import javax.xml.parsers.ParserConfigurationException;
  +import javax.xml.transform.Result;
  +import javax.xml.transform.Transformer;
  +import javax.xml.transform.TransformerFactory;
  +import javax.xml.transform.dom.DOMSource;
  +import javax.xml.transform.stream.StreamResult;
   
  -import org.jdom.input.DOMBuilder;
  -import org.jdom.output.Format;
  -import org.jdom.output.XMLOutputter;
  +import org.apache.juddi.util.Loader;
   import org.w3c.dom.Document;
   import org.w3c.dom.Element;
   import org.w3c.dom.Node;
   import org.w3c.dom.NodeList;
  +import org.xml.sax.SAXException;
  +import org.xml.sax.SAXParseException;
  +import org.xml.sax.helpers.DefaultHandler;
   
   /**
    * @author Steve Viens (sviens@apache.org)
  @@ -100,12 +105,8 @@
   
       try
       {
  -      javax.xml.parsers.DocumentBuilderFactory factory =
  -        javax.xml.parsers.DocumentBuilderFactory.newInstance();
  -
  -      javax.xml.parsers.DocumentBuilder builder =
  -        factory.newDocumentBuilder();
  -
  +      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  +      DocumentBuilder builder = factory.newDocumentBuilder();
         Document document = builder.newDocument();
         Element holder = document.createElement("root");
         document.appendChild(holder);
  @@ -142,36 +143,95 @@
   
     public static void writeXML(Element element,OutputStream stream)
     {
  -      try {
  -          DOMBuilder builder = new DOMBuilder();
  -          Format format = org.jdom.output.Format.getPrettyFormat();
  -          XMLOutputter outputter = new XMLOutputter(format);
  -          outputter.output(builder.build(element),stream);        
  -      }
  -      catch(IOException ioex) {
  -          ioex.printStackTrace();
  -      }
  -  }
  -
  -  public static void writeXML(Element element,Writer writer)
  -  {
  -      try {
  -          DOMBuilder builder = new DOMBuilder();
  -          Format format = org.jdom.output.Format.getPrettyFormat();
  -          XMLOutputter outputter = new XMLOutputter(format);
  -          outputter.output(builder.build(element),writer);        
  -      }
  -      catch(IOException ioex) {
  -          ioex.printStackTrace();
  -      }
  +    try {
  +      TransformerFactory xformerFactory = TransformerFactory.newInstance();
  +      Transformer xformer = xformerFactory.newTransformer();
  +      Result output = new StreamResult(stream);
  +      DOMSource source = new DOMSource(element);
  +      
  +      // print the xml to the specified OutputStream
  +      xformer.transform(source,output);
  +    }
  +    catch(Exception ex) {
  +      ex.printStackTrace();
  +    }
     }
  -  
  +    
     public static String toString(Element element)
     {
  -    StringWriter writer = new StringWriter();
  +      ByteArrayOutputStream stream = new ByteArrayOutputStream();      
  +      writeXML(element,stream);
  +      
  +      return stream.toString();      
  +  }
  +  
  +  public static void validate(URL xmlDocUrl) 
  +  {    
  +    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  +    factory.setValidating(true);    
  +    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage","http://www.w3.org/2001/XMLSchema");
  +    factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaSource",Loader.getResource("uddi_v2.xsd"));
  +
  +    try
  +    {
  +        DocumentBuilder builder = factory.newDocumentBuilder();
  +        Validator handler = new Validator();
  +        builder.setErrorHandler(handler); 
  +        //builder.parse(xmlDocUrl);
       
  -    writeXML(element,writer);
  +        if ((handler.error) || (handler.warning))
  +            System.out.println(handler.toString());
  +    }
  +    catch(ParserConfigurationException pcex) {
  +        pcex.printStackTrace();
  +    }
  +  }
  +}
  +
  +class Validator extends DefaultHandler
  +{
  +  public boolean warning = false;
  +  public boolean error = false;  
  +  public SAXParseException exception = null;
       
  -    return writer.toString();    
  -  } 
  +  public void warning(SAXParseException spex) 
  +      throws SAXException
  +  {
  +    warning = true;
  +    exception = spex;        
  +  }
  +  
  +  public void error(SAXParseException spex) 
  +      throws SAXException
  +  {
  +    error = true;
  +    exception = spex;        
  +  }
  +  
  +  public void fatalError(SAXParseException spex) 
  +      throws SAXException
  +  {
  +    error = true;
  +    exception = spex;        
  +  }
  +  
  +  public String toString()
  +  {
  +    StringBuffer buffer = new StringBuffer();
  +    
  +    if (exception != null)
  +    {
  +      buffer.append("Public ID: " + exception.getPublicId());
  +      buffer.append("\n");
  +      buffer.append("System ID: " + exception.getSystemId());
  +      buffer.append("\n");
  +      buffer.append("Line number: " + exception.getLineNumber());
  +      buffer.append("\n");
  +      buffer.append("Column number: " + exception.getColumnNumber());
  +      buffer.append("\n");
  +      buffer.append("Message: " + exception.getMessage());
  +    }
  +    
  +    return buffer.toString();
  +  }
   }
  \ No newline at end of file
  
  
  

Mime
View raw message