Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2010-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
   *
   * Licensed under the Apache License, Version 2.0 (the "License").
   * You may not use this file except in compliance with the License.
   * A copy of the License is located at
   *
   *  http://aws.amazon.com/apache2.0
   *
  * or in the "license" file accompanying this file. This file 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 com.amazonaws.util;
 
 
FilterInputStream implementation that wraps an InputStream containing an XML document, and removes the XML namespace attribute from the XML document.
 
look ahead buffer
 
     private byte[] lookAheadData = new byte[200];

    
Set to true once the namespace has been removed
 
     private boolean hasRemovedNamespace = false;

    
Constructs a new NamespaceRemovingInputStream wrapping the specified InputStream.

Parameters:
in The InputStream containing an XML document whose XML namespace is to be removed.
 
         // Wrap our input stream in a buffered input stream to ensure
         // that it support mark/reset
         super(new BufferedInputStream(in));
     }
 
     /* (non-Javadoc)
      * @see java.io.FilterInputStream#read()
      */
     @Override
     public int read() throws IOException {
         abortIfNeeded();
         int b = .read();
         if (b == 'x' && !) {
             [0] = (byte)b;
             .mark(.);
             int bytesRead = .read(, 1, . - 1);
             .reset();
 
             String string = new String(, 0, bytesRead + 1);
 
             int numberCharsMatched = matchXmlNamespaceAttribute(string);
             if (numberCharsMatched > 0) {
                 for (int i = 0; i < numberCharsMatched - 1; i++) {
                     .read();
                 }
                 b = .read();
                  = true;
             }
         }
 
         return b;
     }
 
     /* (non-Javadoc)
      * @see java.io.FilterInputStream#read(byte[], int, int)
      */
     @Override
     public int read(byte[] bint offint lenthrows IOException {
         for (int i = 0; i < leni++) {
             int j = this.read();
             if (j == -1) {
                 if (i == 0) return -1;
                 return i;
             }
 
             b[i + off] = (byte)j;
         }
 
         return len;
     }
 
     /* (non-Javadoc)
      * @see java.io.FilterInputStream#read(byte[])
      */
     @Override
     public int read(byte[] bthrows IOException {
        return this.read(b, 0, b.length);
    }

    
Checks if the string starts with a complete XML namespace attribute, and if so, returns the number of characters that match.

Parameters:
s The string to check for an XML namespace definition.
Returns:
-1 if no XML namespace definition was found, otherwise the length of the identified XML namespace definition.
    private int matchXmlNamespaceAttribute(String s) {
        /*
         * The regex we're simulating is: "xmlns\\s*=\\s*\".+?\".*"
         */
        StringPrefixSlicer stringSlicer = new StringPrefixSlicer(s);
        if (stringSlicer.removePrefix("xmlns") == falsereturn -1;
        stringSlicer.removeRepeatingPrefix(" ");
        if (stringSlicer.removePrefix("=") == falsereturn -1;
        stringSlicer.removeRepeatingPrefix(" ");
        if (stringSlicer.removePrefix("\"") == falsereturn -1;
        if (stringSlicer.removePrefixEndingWith("\"") == falsereturn -1;
        return s.length() - stringSlicer.getString().length();
    }

    
Utility class to help test and remove specified prefixes from a string.
    private static final class StringPrefixSlicer {
        private String s;
        public StringPrefixSlicer(String s) {
            this. = s;
        }

        

Returns:
The remaining String (minus any prefixes that have been removed).
        public String getString() {
            return ;
        }
        public boolean removePrefix(String prefix) {
            if (.startsWith(prefix) == falsereturn false;
             = .substring(prefix.length());
            return true;
        }
        public boolean removeRepeatingPrefix(String prefix) {
            if (.startsWith(prefix) == falsereturn false;
            while (.startsWith(prefix)) {
                 = .substring(prefix.length());
            }
            return true;
        }
        public boolean removePrefixEndingWith(String marker) {
            int i = .indexOf(marker);
            if (i < 0) return false;
             = .substring(i + marker.length());
            return true;
        }
    }
New to GrepCode? Check out our FAQ X