Coverage Report - com.jcabi.xml.ClasspathInput
 
Classes in this File Line Coverage Branch Coverage Complexity
ClasspathInput
50%
14/28
50%
1/2
1.294
 
 1  
 /**
 2  
  * Copyright (c) 2012-2017, jcabi.com
 3  
  * All rights reserved.
 4  
  *
 5  
  * Redistribution and use in source and binary forms, with or without
 6  
  * modification, are permitted provided that the following conditions
 7  
  * are met: 1) Redistributions of source code must retain the above
 8  
  * copyright notice, this list of conditions and the following
 9  
  * disclaimer. 2) Redistributions in binary form must reproduce the above
 10  
  * copyright notice, this list of conditions and the following
 11  
  * disclaimer in the documentation and/or other materials provided
 12  
  * with the distribution. 3) Neither the name of the jcabi.com nor
 13  
  * the names of its contributors may be used to endorse or promote
 14  
  * products derived from this software without specific prior written
 15  
  * permission.
 16  
  *
 17  
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18  
  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 19  
  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 20  
  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 21  
  * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 22  
  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 23  
  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 24  
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 25  
  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 26  
  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 27  
  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 28  
  * OF THE POSSIBILITY OF SUCH DAMAGE.
 29  
  */
 30  
 package com.jcabi.xml;
 31  
 
 32  
 import java.io.IOException;
 33  
 import java.io.InputStream;
 34  
 import java.io.Reader;
 35  
 import java.nio.charset.Charset;
 36  
 import org.apache.commons.io.IOUtils;
 37  
 import org.w3c.dom.ls.LSInput;
 38  
 
 39  
 /**
 40  
  * {@link org.w3c.dom.ls.LSInput} implementation
 41  
  * used by {@link com.jcabi.xml.ClasspathResolver}.
 42  
  *
 43  
  * @author Adam Siemion (adam.siemion.null@lemonsoftware.pl)
 44  
  * @version $Id: 6e79cd9e62d6790c29396b06cd30ea0dfefb4af3 $
 45  
  */
 46  
 class ClasspathInput implements LSInput {
 47  
 
 48  
     /**
 49  
      * Public Id.
 50  
      */
 51  
     private transient String publicid;
 52  
     /**
 53  
      * System Id.
 54  
      */
 55  
     private transient String systemid;
 56  
 
 57  
     /**
 58  
      * Constructor.
 59  
      * @param pubid Public id
 60  
      * @param sysid System id
 61  
      */
 62  2
     ClasspathInput(final String pubid, final String sysid) {
 63  2
         this.publicid = pubid;
 64  2
         this.systemid = sysid;
 65  2
     }
 66  
 
 67  
     @Override
 68  
     public String getPublicId() {
 69  1
         return this.publicid;
 70  
     }
 71  
 
 72  
     @Override
 73  
     public void setPublicId(final String pubid) {
 74  0
         this.publicid = pubid;
 75  0
     }
 76  
 
 77  
     @Override
 78  
     public String getSystemId() {
 79  1
         return this.systemid;
 80  
     }
 81  
 
 82  
     @Override
 83  
     public void setSystemId(final String sysid) {
 84  0
         this.systemid = sysid;
 85  0
     }
 86  
 
 87  
     @Override
 88  
     public String getBaseURI() {
 89  1
         return null;
 90  
     }
 91  
 
 92  
     @Override
 93  
     public InputStream getByteStream() {
 94  1
         return null;
 95  
     }
 96  
 
 97  
     @Override
 98  
     @SuppressWarnings("PMD.BooleanGetMethodName")
 99  
     public boolean getCertifiedText() {
 100  0
         return false;
 101  
     }
 102  
 
 103  
     @Override
 104  
     public Reader getCharacterStream() {
 105  1
         return null;
 106  
     }
 107  
 
 108  
     @Override
 109  
     public String getEncoding() {
 110  1
         return null;
 111  
     }
 112  
 
 113  
     @Override
 114  
     public String getStringData() {
 115  2
         final InputStream stream = getClass().getResourceAsStream(
 116  
             this.systemid
 117  
         );
 118  2
         if (stream == null) {
 119  0
             throw new IllegalArgumentException(
 120  
                 String.format(
 121  
                     "Systemid %s resource does not exist or can't be opened.",
 122  
                     this.systemid
 123  
                 )
 124  
             );
 125  
         }
 126  
         try {
 127  2
             return IOUtils.toString(stream, Charset.forName("UTF-8"));
 128  0
         } catch (final IOException exception) {
 129  0
             throw new IllegalArgumentException(
 130  
                 String.format(
 131  
                     "Unable to read input stream of systemid %s", this.systemid
 132  
                 ),
 133  
                 exception
 134  
             );
 135  
         } finally {
 136  2
             IOUtils.closeQuietly(stream);
 137  
         }
 138  
     }
 139  
 
 140  
     @Override
 141  
     public void setBaseURI(final String baseuri) {
 142  
         // intentionally empty
 143  0
     }
 144  
 
 145  
     @Override
 146  
     public void setByteStream(final InputStream bytestream) {
 147  
         // intentionally empty
 148  0
     }
 149  
 
 150  
     @Override
 151  
     public void setCertifiedText(final boolean certifiedtext) {
 152  
         // intentionally empty
 153  0
     }
 154  
 
 155  
     @Override
 156  
     public void setCharacterStream(final Reader characterstream) {
 157  
         // intentionally empty
 158  0
     }
 159  
 
 160  
     @Override
 161  
     public void setEncoding(final String encoding) {
 162  
         // intentionally empty
 163  0
     }
 164  
 
 165  
     @Override
 166  
     public void setStringData(final String stringdata) {
 167  
         // intentionally empty
 168  0
     }
 169  
 }