1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 2005 The Apache Software Foundation.
   7  *
   8  * Licensed under the Apache License, Version 2.0 (the "License");
   9  * you may not use this file except in compliance with the License.
  10  * You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.jaxp.validation;
  22 
  23 import javax.xml.transform.stream.StreamSource;
  24 
  25 import com.sun.org.apache.xerces.internal.xni.XNIException;
  26 import com.sun.org.apache.xerces.internal.xni.parser.XMLInputSource;
  27 import com.sun.org.apache.xerces.internal.xni.parser.XMLParseException;
  28 import org.xml.sax.SAXException;
  29 import org.xml.sax.SAXParseException;
  30 
  31 /**
  32  * <p>Static utility methods for the Validation API implementation.</p>
  33  *
  34  * @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
  35  */
  36 final class Util {
  37 
  38     /**
  39      * Creates a proper {@link XMLInputSource} from a {@link StreamSource}.
  40      *
  41      * @return always return non-null valid object.
  42      */
  43     public static final XMLInputSource toXMLInputSource( StreamSource in ) {
  44         if( in.getReader()!=null )
  45             return new XMLInputSource(
  46             in.getPublicId(), in.getSystemId(), in.getSystemId(),
  47             in.getReader(), null );
  48         if( in.getInputStream()!=null )
  49             return new XMLInputSource(
  50             in.getPublicId(), in.getSystemId(), in.getSystemId(),
  51             in.getInputStream(), null );
  52 
  53         return new XMLInputSource(
  54         in.getPublicId(), in.getSystemId(), in.getSystemId() );
  55     }
  56 
  57     /**
  58      * Reconstructs {@link SAXException} from XNIException.
  59      */
  60     public static SAXException toSAXException(XNIException e) {
  61         if(e instanceof XMLParseException)
  62             return toSAXParseException((XMLParseException)e);
  63         if( e.getException() instanceof SAXException )
  64             return (SAXException)e.getException();
  65         return new SAXException(e.getMessage(),e.getException());
  66     }
  67 
  68     public static SAXParseException toSAXParseException( XMLParseException e ) {
  69         if( e.getException() instanceof SAXParseException )
  70             return (SAXParseException)e.getException();
  71         return new SAXParseException( e.getMessage(),
  72         e.getPublicId(), e.getExpandedSystemId(),
  73         e.getLineNumber(), e.getColumnNumber(),
  74         e.getException() );
  75     }
  76 
  77 } // Util