Domain.java :  » Content-Management-System » harmonise » org » openharmonise » vfs » metadata » Java Open Source

Java Open Source » Content Management System » harmonise 
harmonise » org » openharmonise » vfs » metadata » Domain.java
/*
 * The contents of this file are subject to the 
 * Mozilla Public License Version 1.1 (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.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
 * See the License for the specific language governing rights and 
 * limitations under the License.
 *
 * The Initial Developer of the Original Code is Simulacra Media Ltd.
 * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
 *
 * All Rights Reserved.
 *
 * Contributor(s):
 */
package org.openharmonise.vfs.metadata;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

/**
 * Domains describe the applicability of properties by stating the paths
 * that they are allowed to be used in and the min and max occurrence values
 * for that path and any resource/content type restrictions. Domains are
 * stored as metadata on the {@link org.openharmonise.vfs.VirtualFile}s for {@link Property} objects. 
 * 
 * @author Matthew Large
 * @version $Revision: 1.1 $
 *
 */
public class Domain {

  /**
   * Allowed resource type.
   */
  private String m_sResourceType = null;
  
  /**
   * Allowed content types.
   */
  private ArrayList m_aContentTypes = new ArrayList(3);
  
  /**
   * Allowed paths.
   */
  private ArrayList m_aPaths = new ArrayList(3);
  
  /**
   * Minimum occurrence.
   */
  private int m_nMinOccurs = 0;
  
  /**
   * Maximum occurrence. A value of -1 represents UNBOUNDED.
   */
  private int m_nMaxOccurs = -1;

  /**
   * Constructs a new Domain.
   */
  public Domain() {
    super();
  }
  
  

  /**
   * Returns the maximum occurrence.
   * 
   * @return Maximum Occurrence
   */
  public int getMaxOccurs() {
    return m_nMaxOccurs;
  }

  /**
   * Returns the minimum occurrence.
   * 
   * @return Minimum occurrence
   */
  public int getMinOccurs() {
    return m_nMinOccurs;
  }

  /**
   * Returns a list of the allowed content types.
   * 
   * @return Content types
   */
  public List getContentTypes() {
    return (List)m_aContentTypes.clone();
  }

  /**
   * Allowed resource type.
   * 
   * @return Resource type
   */
  public String getResourceType() {
    return m_sResourceType;
  }

  /**
   * Sets the maximum occurrence.
   * 
   * @param i Maximum occurrence
   */
  public void setMaxOccurs(int i) {
    m_nMaxOccurs = i;
  }

  /**
   * Sets the minimum occurrence.
   * 
   * @param i Minimum occurrence
   */
  public void setMinOccurs(int i) {
    m_nMinOccurs = i;
  }

  /**
   * Adds an allowed content type.
   * 
   * @param string Content type
   */
  public void addContentType(String string) {
    m_aContentTypes.add( string);
  }

  /**
   * Sets the allowed resource type.
   * 
   * @param string Resource type
   */
  public void setResourceType(String string) {
    m_sResourceType = string;
  }

  /**
   * Adds an allowed path.
   * 
   * @param sPath Full path
   */
  public void addPath(String sPath) {
    this.m_aPaths.add(sPath);
  }
  
  /**
   * Returns a list of all the allowed paths.
   * 
   * @return Full paths
   */
  public List getPaths() {
    return (List) this.m_aPaths.clone();
  }
  
  /**
   * Validates a list of values against the restrictions in this domain.
   * 
   * @param aValues List of values to be validated
   * @return Results of the validation
   */
  public ValidationResult validate(List aValues) {
    ValidationResult result = new ValidationResult();
    
    if( aValues.size()< this.m_nMinOccurs ) {
      result.setValid(false);
      result.setMessage(this.m_nMinOccurs + " is the minimum number of values required for this property.");
    }
    
    if( this.m_nMaxOccurs>-1 && aValues.size()>this.m_nMaxOccurs) {
      result.setValid(false);
      result.setMessage(this.m_nMaxOccurs + " is the maximum number of values allowed for this property.");
    }
    
    return result;
  }
  
  /**
   * Populates the domain from a XML element.
   * 
   * @param elDomain Root element of domain XML
   */
  public void instantiate(Element elDomain) {
    NodeList nl = elDomain.getElementsByTagNameNS("DAV:", "href");
    for(int i=0; i<nl.getLength(); i++) {
      Element elPath = (Element)nl.item(i);
      if(elPath.getChildNodes().getLength()==1) {
        Node node = elPath.getFirstChild();
        if(node.getNodeType()==Node.TEXT_NODE) {
          this.m_aPaths.add( ((Text)node).getNodeValue() );
        }
      }
    }

    nl = elDomain.getElementsByTagNameNS("DAV:", "resourcetype");
    for(int i=0; i<nl.getLength(); i++) {
      Element elResourceType = (Element)nl.item(i);
      if(elResourceType.getChildNodes().getLength()>0) {
        NodeList nl2 = elResourceType.getChildNodes();
        for(int j=0; j<nl2.getLength(); j++) {
          //this.m_sResourceType = nl2.item(j).getNodeName();
          if(nl2.item(j).getNodeType()==Node.ELEMENT_NODE) {
            this.m_sResourceType = ((Element)nl2.item(j)).getTagName();
          }
        }
      }
    }

    nl = elDomain.getElementsByTagNameNS("DAV:", "contenttype");
    for(int i=0; i<nl.getLength(); i++) {
      Element elContentType = (Element)nl.item(i);
      if(elContentType.getChildNodes().getLength()==1) {
        Node node = elContentType.getFirstChild();
        if(node.getNodeType()==Node.TEXT_NODE) {
          this.m_aContentTypes.add( ((Text)node).getNodeValue() );
        }
      }
    }

    nl = elDomain.getElementsByTagNameNS("DAV:", "minOccurs");
    for(int i=0; i<nl.getLength(); i++) {
      Element elMinOccurs = (Element)nl.item(i);
      if(elMinOccurs.getChildNodes().getLength()==1) {
        Node node = elMinOccurs.getFirstChild();
        if(node.getNodeType()==Node.TEXT_NODE) {
          this.m_nMinOccurs = Integer.parseInt( ((Text)node).getNodeValue() );
        }
      }
    }

    nl = elDomain.getElementsByTagNameNS("DAV:", "maxOccurs");
    for(int i=0; i<nl.getLength(); i++) {
      Element elMaxOccurs = (Element)nl.item(i);
      if(elMaxOccurs.getChildNodes().getLength()==1) {
        Node node = elMaxOccurs.getFirstChild();
        if(node.getNodeType()==Node.TEXT_NODE) {
          this.m_nMaxOccurs = Integer.parseInt( ((Text)node).getNodeValue() );
        }
      }
    }
  }

  public String toString() {
    StringBuffer sBuff = new StringBuffer();
    
    sBuff.append("Domain:\n")
       .append("Resourcetype: ").append(this.m_sResourceType).append("\n")
       .append("MinOccurs: ").append(this.m_nMinOccurs).append("\n")
       .append("MaxOccurs: ").append(this.m_nMaxOccurs).append("\n");
       
    Iterator itor = this.m_aContentTypes.iterator();
    while(itor.hasNext()) {
      sBuff.append("Contenttype: ").append(((String)itor.next())).append("\n");
    }
    
    itor = this.m_aPaths.iterator();
    while(itor.hasNext()) {
      sBuff.append("Path: ").append(((String)itor.next())).append("\n");
    }
    
    return sBuff.toString();
  }

}
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.