1 /*
   2  * Copyright (c) 1997, 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.tools.internal.ws.processor.model;
  27 
  28 import com.sun.tools.internal.ws.wsdl.framework.Entity;
  29 import com.sun.tools.internal.ws.wscompile.ErrorReceiver;
  30 import com.sun.tools.internal.ws.wscompile.AbortException;
  31 import com.sun.tools.internal.ws.resources.ModelMessages;
  32 
  33 import javax.xml.namespace.QName;
  34 import java.util.*;
  35 
  36 /**
  37  *
  38  * @author WS Development Team
  39  */
  40 public abstract class Message extends ModelObject {
  41     protected Message(com.sun.tools.internal.ws.wsdl.document.Message entity, ErrorReceiver receiver) {
  42         super(entity);
  43         setErrorReceiver(receiver);
  44     }
  45 
  46     public void addBodyBlock(Block b) {
  47         if (_bodyBlocks.containsKey(b.getName())) {
  48             errorReceiver.error(getEntity().getLocator(), ModelMessages.MODEL_PART_NOT_UNIQUE(((com.sun.tools.internal.ws.wsdl.document.Message)getEntity()).getName(), b.getName()));
  49             throw new AbortException();
  50         }
  51         _bodyBlocks.put(b.getName(), b);
  52         b.setLocation(Block.BODY);
  53     }
  54 
  55     public Iterator<Block> getBodyBlocks() {
  56         return _bodyBlocks.values().iterator();
  57     }
  58 
  59     public int getBodyBlockCount() {
  60         return _bodyBlocks.size();
  61     }
  62 
  63     /* serialization */
  64     public Map<QName, Block> getBodyBlocksMap() {
  65         return _bodyBlocks;
  66     }
  67 
  68     /* serialization */
  69     public void setBodyBlocksMap(Map<QName, Block> m) {
  70         _bodyBlocks = m;
  71     }
  72 
  73     public boolean isBodyEmpty() {
  74         return getBodyBlocks().hasNext();
  75     }
  76 
  77     public boolean isBodyEncoded() {
  78         boolean isEncoded = false;
  79         for (Iterator iter = getBodyBlocks(); iter.hasNext();) {
  80             Block bodyBlock = (Block) iter.next();
  81             if (bodyBlock.getType().isSOAPType()) {
  82                 isEncoded = true;
  83             }
  84         }
  85         return isEncoded;
  86     }
  87 
  88     public void addHeaderBlock(Block b) {
  89         if (_headerBlocks.containsKey(b.getName())) {
  90             errorReceiver.error(getEntity().getLocator(), ModelMessages.MODEL_PART_NOT_UNIQUE(((com.sun.tools.internal.ws.wsdl.document.Message)getEntity()).getName(), b.getName()));
  91             throw new AbortException();
  92         }
  93         _headerBlocks.put(b.getName(), b);
  94         b.setLocation(Block.HEADER);
  95     }
  96 
  97     public Iterator<Block> getHeaderBlocks() {
  98         return _headerBlocks.values().iterator();
  99     }
 100 
 101     public Collection<Block> getHeaderBlockCollection() {
 102         return _headerBlocks.values();
 103     }
 104 
 105     public int getHeaderBlockCount() {
 106         return _headerBlocks.size();
 107     }
 108 
 109     /* serialization */
 110     public Map<QName, Block> getHeaderBlocksMap() {
 111         return _headerBlocks;
 112     }
 113 
 114     /* serialization */
 115     public void setHeaderBlocksMap(Map<QName, Block> m) {
 116         _headerBlocks = m;
 117     }
 118 
 119     /** attachment block */
 120     public void addAttachmentBlock(Block b) {
 121         if (_attachmentBlocks.containsKey(b.getName())) {
 122             errorReceiver.error(getEntity().getLocator(), ModelMessages.MODEL_PART_NOT_UNIQUE(((com.sun.tools.internal.ws.wsdl.document.Message)getEntity()).getName(), b.getName()));
 123             throw new AbortException();
 124         }
 125         _attachmentBlocks.put(b.getName(), b);
 126         b.setLocation(Block.ATTACHMENT);
 127     }
 128 
 129     public void addUnboundBlock(Block b) {
 130         if (_unboundBlocks.containsKey(b.getName())) {
 131             return;
 132         }
 133         _unboundBlocks.put(b.getName(), b);
 134         b.setLocation(Block.UNBOUND);
 135     }
 136 
 137     public Iterator<Block> getUnboundBlocks() {
 138         return _unboundBlocks.values().iterator();
 139     }
 140 
 141     /* serialization */
 142     public Map<QName, Block> getUnboundBlocksMap() {
 143         return _unboundBlocks;
 144     }
 145 
 146     public int getUnboundBlocksCount() {
 147         return _unboundBlocks.size();
 148     }
 149 
 150     /* serialization */
 151     public void setUnboundBlocksMap(Map<QName, Block> m) {
 152         _unboundBlocks = m;
 153     }
 154 
 155 
 156     public Iterator<Block> getAttachmentBlocks() {
 157         return _attachmentBlocks.values().iterator();
 158     }
 159 
 160     public int getAttachmentBlockCount () {
 161         return _attachmentBlocks.size();
 162     }
 163 
 164         /* serialization */
 165     public Map<QName, Block> getAttachmentBlocksMap() {
 166         return _attachmentBlocks;
 167     }
 168 
 169     /* serialization */
 170     public void setAttachmentBlocksMap(Map<QName, Block> m) {
 171         _attachmentBlocks = m;
 172     }
 173 
 174     public void addParameter(Parameter p) {
 175         if (_parametersByName.containsKey(p.getName())) {
 176             errorReceiver.error(getEntity().getLocator(), ModelMessages.MODEL_PARAMETER_NOTUNIQUE(p.getName(), p.getName()));
 177             throw new AbortException();
 178         }
 179         _parameters.add(p);
 180         _parametersByName.put(p.getName(), p);
 181     }
 182 
 183     public Parameter getParameterByName(String name) {
 184         if (_parametersByName.size() != _parameters.size()) {
 185             initializeParametersByName();
 186         }
 187         return _parametersByName.get(name);
 188     }
 189 
 190     public Iterator<Parameter> getParameters() {
 191         return _parameters.iterator();
 192     }
 193 
 194     /* serialization */
 195     public List<Parameter> getParametersList() {
 196         return _parameters;
 197     }
 198 
 199     /* serialization */
 200     public void setParametersList(List<Parameter> l) {
 201         _parameters = l;
 202     }
 203 
 204     private void initializeParametersByName() {
 205         _parametersByName = new HashMap();
 206         if (_parameters != null) {
 207             for (Iterator iter = _parameters.iterator(); iter.hasNext();) {
 208                 Parameter param = (Parameter) iter.next();
 209                 if (param.getName() != null &&
 210                     _parametersByName.containsKey(param.getName())) {
 211                     errorReceiver.error(getEntity().getLocator(), ModelMessages.MODEL_PARAMETER_NOTUNIQUE(param.getName(), param.getName()));
 212                     throw new AbortException();
 213                 }
 214                 _parametersByName.put(param.getName(), param);
 215             }
 216         }
 217     }
 218 
 219     public Set<Block> getAllBlocks(){
 220         Set<Block> blocks = new HashSet<Block>();
 221         blocks.addAll(_bodyBlocks.values());
 222         blocks.addAll(_headerBlocks.values());
 223         blocks.addAll(_attachmentBlocks.values());
 224         return blocks;
 225     }
 226 
 227     private Map<QName, Block> _attachmentBlocks = new HashMap<QName, Block>();
 228     private Map<QName, Block> _bodyBlocks = new HashMap<QName, Block>();
 229     private Map<QName, Block> _headerBlocks = new HashMap<QName, Block>();
 230     private Map<QName, Block> _unboundBlocks = new HashMap<QName, Block>();
 231     private List<Parameter> _parameters = new ArrayList<Parameter>();
 232     private Map<String, Parameter> _parametersByName = new HashMap<String, Parameter>();
 233 }