XmlParameterResource.java :  » XML » activexml » org » xdev » base » resource » xml » Java Open Source

Java Open Source » XML » activexml 
activexml » org » xdev » base » resource » xml » XmlParameterResource.java
/*
 * Created on Feb 13, 2004
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package org.xdev.base.resource.xml;

import java.util.HashMap;

import org.jaxen.jdom.JDOMXPath;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.xdev.base.core.compiler.AXCompiler;
import org.xdev.base.resource.AbstractDataResource;
import org.xdev.base.xssl.XSSLAction;
import org.xdev.base.xssl.XSSLReturn;

/**
 * @author AYegorov
 *
 * To change the template for this generated type comment go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
public class XmlParameterResource extends AbstractDataResource {
  protected Element root = null;
  
  private StringBuffer buffer = new StringBuffer();
  
  private Object returnValue = null;

  /**
   * @param id
   */
  public XmlParameterResource(String id) {
    super(id);
    // XXX Auto-generated constructor stub
  }
  /**
   * @param id
   * @param properties
   */
  public XmlParameterResource(String id, HashMap properties) {
    super(id, properties);
    // XXX Auto-generated constructor stub
  }
  
  /* (non-Javadoc)
   * @see org.xdev.base.resource.AbstractDataResource#initialize()
   */
  protected void initialize() throws Exception {
    this.root = ((Document)this.getResourceConnection()).getRootElement();
  }
  
  protected void set() throws Exception {
        if (this.hasProperty("path")) {
            super.ds = new XmlDataSource();
            
            super.ds.setProperty("path", this.getProperty("path"));
            
            super.ds.initialize();
            
            String dataSourceId = this.getProperty("datasource-id");
            
            if ("".equals(dataSourceId)) {
                dataSourceId = this.getReferenceId();
            }
            
            if (dataSourceId != null && !"".equals(dataSourceId)) {
                AXCompiler.getInstance().addDataSource(dataSourceId, super.ds);
            }
        }

        super.set();
  }
  
  protected void invoke() throws Exception {
    if (this.hasProperty("expression")) {
        this.buffer.setLength(0);
        
        this.buffer.append(this.getProperty("expression"));
    }
    
    this.returnValue = this.search(new JDOMXPath(this.buffer.toString()));
  }
  
  /* (non-Javadoc)
   * @see org.xdev.base.xssl.XSSLActionIterator#invoke(org.xdev.base.xssl.XSSLAction)
   */
  protected void invoke(XSSLAction component) throws Exception {
    this.buffer.append(((XSSLReturn)component).getObjectValue());
  }
  
  protected String search(JDOMXPath xpath) throws Exception{
    Object obj  = xpath.selectSingleNode(this.root);
    
    String value = "";
    
    if (obj instanceof Element) {
      value = ((Element)obj).getTextTrim();
    }
    else if (obj instanceof Attribute) {
      value = ((Attribute)obj).getValue();
    }
  
    return value;
  }

  
  public Object getObjectValue() {
    return this.returnValue;
  }
  
  /* (non-Javadoc)
   * @see org.xdev.base.xssl.manage.TransactionFinalizable#doFinalize()
   */
  public void doFinalize() throws Exception {}
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.