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