1 /*
   2  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.xml.internal.stream.buffer.stax;
  27 
  28 import com.sun.xml.internal.stream.buffer.AbstractCreator;
  29 import java.util.ArrayList;
  30 import java.util.List;
  31 
  32 /**
  33  * {@link AbstractCreator} with additional convenience code.
  34  *
  35  * @author Paul Sandoz
  36  * @author Venu
  37  * @author Kohsuke Kawaguchi
  38  */
  39 abstract class StreamBufferCreator extends AbstractCreator {
  40 
  41     private boolean checkAttributeValue = false;
  42 
  43     protected List<String> attributeValuePrefixes = new ArrayList<String>();
  44 
  45     protected void storeQualifiedName(int item, String prefix, String uri, String localName) {
  46         if (uri != null && uri.length() > 0) {
  47             if (prefix != null && prefix.length() > 0) {
  48                 item |= FLAG_PREFIX;
  49                 storeStructureString(prefix);
  50             }
  51 
  52             item |= FLAG_URI;
  53             storeStructureString(uri);
  54         }
  55 
  56         storeStructureString(localName);
  57 
  58         storeStructure(item);
  59     }
  60 
  61     protected final void storeNamespaceAttribute(String prefix, String uri) {
  62         int item = T_NAMESPACE_ATTRIBUTE;
  63 
  64         if (prefix != null && prefix.length() > 0) {
  65             item |= FLAG_PREFIX;
  66             storeStructureString(prefix);
  67         }
  68 
  69         if (uri != null && uri.length() > 0) {
  70             item |= FLAG_URI;
  71             storeStructureString(uri);
  72         }
  73 
  74         storeStructure(item);
  75     }
  76 
  77     protected final void storeAttribute(String prefix, String uri, String localName, String type, String value) {
  78         storeQualifiedName(T_ATTRIBUTE_LN, prefix, uri, localName);
  79 
  80         storeStructureString(type);
  81         storeContentString(value);
  82         if(checkAttributeValue && value.indexOf("://") == -1){  // the condition after && avoids looking inside URIs
  83             int firstIndex = value.indexOf(":");
  84             int lastIndex = value.lastIndexOf(":");  // Check last index of : as some SAML namespace have multiple ":"s
  85             if(firstIndex != -1 && lastIndex == firstIndex){
  86                 String valuePrefix = value.substring(0, firstIndex);
  87                 if(!attributeValuePrefixes.contains(valuePrefix)){
  88                     attributeValuePrefixes.add(valuePrefix);
  89                 }
  90             }
  91         }
  92     }
  93 
  94     public final List getAttributeValuePrefixes(){
  95         return attributeValuePrefixes;
  96     }
  97 
  98     protected final void storeProcessingInstruction(String target, String data) {
  99         storeStructure(T_PROCESSING_INSTRUCTION);
 100         storeStructureString(target);
 101         storeStructureString(data);
 102     }
 103 
 104     public final boolean isCheckAttributeValue(){
 105         return checkAttributeValue;
 106     }
 107 
 108     public final void setCheckAttributeValue(boolean value){
 109         this.checkAttributeValue = value;
 110     }
 111 }