Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   * 
  *   http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing,
  * software distributed under the License 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 org.apache.tuscany.sca.policy.xml;
 
 import static org.apache.tuscany.sca.policy.xml.PolicyConstants.SCA11_NS;
 
 import java.util.List;
 
 
Processor for handling XML models of ExternalAttachment definitions

Version:
$Rev: 961010 $ $Date: 2010-07-06 13:34:54 -0700 (Tue, 06 Jul 2010) $
 
 
 	
 	
 		this. = factories.getFactory(PolicyFactory.class);
 		this. = XPathHelper.getInstance(registry);
 	}
 	
 		return ExternalAttachment.class;
 	}
 
 	public void resolve(ExternalAttachment attachmentModelResolver resolver,
 	      if (attachment != null && attachment.isUnresolved()) {
 	            resolveIntents(attachmentresolvercontext);
 	            resolvePolicySets(attachmentresolvercontext);	           		          
 
 	            attachment.setUnresolved(false);
 	      }
 		
 	}
 
 	private void resolvePolicySets(ExternalAttachment attachment,
 			ModelResolver resolverProcessorContext context) {
 	     List<PolicySetreferredPolicySets = new ArrayList<PolicySet>();
 	        for (PolicySet referredPolicySet : attachment.getPolicySets()) {
 	            if (referredPolicySet.isUnresolved()) {
 	                PolicySet resolved = resolver.resolveModel(PolicySet.classreferredPolicySetcontext);
 	                if (!resolved.isUnresolved() || resolved != referredPolicySet) {
 	                    referredPolicySets.add(resolved);
 	                } else {
 	                    error(context.getMonitor(), "ReferredPolicySetNotFound"attachmentreferredPolicySetattachment);
 	                    return;	                 
	                }
	            } else {
	                referredPolicySets.add(referredPolicySet);
	            }
	        }
	        attachment.getPolicySets().clear();
	        attachment.getPolicySets().addAll(referredPolicySets);
	}
	private void resolveIntents(ExternalAttachment attachment,
			ModelResolver resolverProcessorContext context) {
	       if (attachment != null) {
	            //resolve all provided intents
	            List<IntentprovidedIntents = new ArrayList<Intent>();
	            for (Intent providedIntent : attachment.getIntents()) {
	                if (providedIntent.isUnresolved()) {
	                    Intent resolved = resolver.resolveModel(Intent.classprovidedIntentcontext);
	                    if (!resolved.isUnresolved() || resolved != providedIntent) {
	                        providedIntents.add(resolved);
	                    } else {
	                        error(context.getMonitor(), "ProvidedIntentNotFound"attachmentprovidedIntentattachment);
	                        return;	                      
	                    }
	                } else {
	                    providedIntents.add(providedIntent);
	                }
	            }
	            attachment.getIntents().clear();
	            attachment.getIntents().addAll(providedIntents);
	        }
	}
	public QName getArtifactType() {
	}
			readPolicySets(attachmentreader);
			readIntents(attachmentreadercontext);
			readAttachTo(attachmentreadercontext);
			return attachment;
	}
	private void readIntents(ExternalAttachment attachment,
			XMLStreamReader readerProcessorContext context) {
	      String value = reader.getAttributeValue(null);
	        if (value != null) {
	            List<Intentintents = attachment.getIntents();
	            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
	                QName qname = getQNameValue(readertokens.nextToken());
	                Intent intent = .createIntent();
	                intent.setName(qname);	               
	                intents.add(intent);
	            }
	        }
	}
	private void readAttachTo(ExternalAttachment attachment,
			XMLStreamReader readerProcessorContext context) {
		Monitor monitor = context.getMonitor();
		String attachTo = reader.getAttributeValue(null);
		if ( attachTo != null ) {
		   try {
               XPath path = .newXPath();
               NamespaceContext nsContext = .getNamespaceContext(attachToreader.getNamespaceContext());
               path.setXPathFunctionResolver(new PolicyXPathFunctionResolver(nsContext));                
                                          
               attachTo = PolicyXPathFunction.normalize(attachTo,getSCAPrefix(nsContext));
               XPathExpression expression = .compile(pathnsContextattachTo);
               attachment.setAttachTo(attachTo);
               attachment.setAttachToXPathExpression(expression);
           } catch (XPathExpressionException e) {
               ContributionReadException ce = new ContributionReadException(e);
               error(monitor"ContributionReadException"attachmentce);              
           }
		}
	}
	   private String getSCAPrefix(NamespaceContext nsContext) {
	        Iterator<Stringiter = nsContext.getPrefixes();
	        while (iter.hasNext()) {
	            String prefix = iter.next();
	            if (!prefix.equals(.)) {
	                return prefix;
	            }
	        }
	        return "__sca";
	    }
Reads policy sets associated with an external attachment element.

Parameters:
subject
operation
reader
    private void readPolicySets(ExternalAttachment attachmentXMLStreamReader reader) {
      
     
        String value = reader.getAttributeValue(null);
        if (value != null) {
            List<PolicySetpolicySets = attachment.getPolicySets();
            for (StringTokenizer tokens = new StringTokenizer(value); tokens.hasMoreTokens();) {
                QName qname = getQNameValue(readertokens.nextToken());
                PolicySet policySet = .createPolicySet();
                policySet.setName(qname);              
                policySets.add(policySet);
            }
        }
    }
	public void write(ExternalAttachment attachmentXMLStreamWriter writer,
		writePolicySets(attachmentwritercontext);
		writeIntents(attachmentwritercontext);
		writeAttachTo(attachmentwritercontext);
	}
	   private void writeAttachTo(ExternalAttachment attachment,
			XMLStreamWriter writerProcessorContext contextthrows XMLStreamException {
		if ( attachment.getAttachTo() != null ) {
		}
	}
	private void writeIntents(ExternalAttachment attachment,
			XMLStreamWriter writerProcessorContext contextthrows XMLStreamException {
	     if (!attachment.getIntents().isEmpty()) {
	    	 StringBuffer sb = new StringBuffer();
	    	 for (Intent intent : attachment.getIntents()) {
	    		 sb.append(getQualifiedName(intent.getName(), writer));
	    		 sb.append(" ");
	    	 }
	    	 // Remove the last space
	    	 sb.deleteCharAt(sb.length() - 1);
	     }
	}
	private void writePolicySets(ExternalAttachment attachment,
			XMLStreamWriter writerProcessorContext contextthrows XMLStreamException {
		if ( !attachment.getPolicySets().isEmpty()) {
			 StringBuffer sb = new StringBuffer();
	    	 for (PolicySet ps : attachment.getPolicySets()) {
	    		 sb.append(getQualifiedName(ps.getName(), writer));
	    		 sb.append(" ");
	    	 }
	    	 // Remove the last space
	    	 sb.deleteCharAt(sb.length() - 1);
		}
	}
	  private String getQualifiedName(QName nameXMLStreamWriter writerthrows XMLStreamException {
	        String local = name.getLocalPart();
	        String prefix = writer.getPrefix(name.getNamespaceURI());
	        if (prefix != null && prefix.length() > 0) {
	            return prefix + ':' + local;
	        } else {
	            return local;
	        }
	    }
  
Report a error.

Parameters:
problems
message
model
    private void error(Monitor monitorString messageObject modelObject... messageParameters) {
        if (monitor != null) {
            Problem problem =
                monitor.createProblem(this.getClass().getName(),
                                      .,
                                      .,
                                      model,
                                      message,
                                      (Object[])messageParameters);
            monitor.problem(problem);
        }
    }
New to GrepCode? Check out our FAQ X