001: /*
002: * Copyright 2005-2007 The Kuali Foundation.
003: *
004: *
005: * Licensed under the Educational Community License, Version 1.0 (the "License");
006: * you may not use this file except in compliance with the License.
007: * You may obtain a copy of the License at
008: *
009: * http://www.opensource.org/licenses/ecl1.php
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package mocks.elements;
018:
019: import org.jdom.Element;
020:
021: import edu.iu.uis.eden.exception.InvalidXmlException;
022: import edu.iu.uis.eden.services.InconsistentDocElementStateException;
023:
024: /**
025: * <p>Title: DocElementValidator </p>
026: * <p>Description: Used to do simple validation on objects implementing
027: * IDocElement interface. To be used as a composite preventing the need
028: * for repeated validation to be used in a superclass or individual implementations.</p>
029: * <p>Copyright: Copyright (c) 2002</p>
030: * <p>Company: Indiana University</p>
031: * @author Ryan Kirkendall
032: * @version 1.0
033: */
034: public class DocElementValidator {
035: private static final org.apache.log4j.Logger LOG = org.apache.log4j.Logger
036: .getLogger(DocElementValidator.class);
037:
038: /**
039: * Checks if loadFromXMLContent methods on standard IDocElement implementations
040: * should return with no work done. Also throws common exceptions.
041: *
042: * @param docElement the doc element to be loaded
043: * @param element the element with the content containing the object's value(s)
044: * @param allowBlank flags whether the object should throw an exception or
045: * continue running if the element passed in is blank
046: * @return boolean notifying the calling IDocElement to return with no work done.
047: * This would be allowBlanks is true and element is blank (null)
048: * @throws InvalidXmlException throws if the passed in element is not the correct
049: * element the object loads from
050: * @throws InconsistentDocElementStateException throws if blanks should not be
051: * allowed and the element passed in is blank (null)
052: */
053: public static boolean returnWithNoWorkDone(IDocElement docElement,
054: Element element, boolean allowBlank)
055: throws InvalidXmlException,
056: InconsistentDocElementStateException {
057: LOG.debug("returnWithNoWorkDone");
058:
059: return isElementPresent(docElement.getElementName(), element,
060: allowBlank);
061: }
062:
063: /**
064: * Checks if loadFromXMLContent methods on standard IDocElement implementations
065: * should return with no work done. Also throws common exceptions.
066: *
067: * @param docElement the doc element to be loaded
068: * @param element the element with the content containing the object's value(s)
069: * @param allowBlank flags whether the object should throw an exception or
070: * continue running if the element passed in is blank
071: * @param elementName name the element containing the IDocElement values should
072: * have
073: * @return boolean notifying the calling IDocElement to return with no work done.
074: * This would be allowBlanks is true and element is blank (null)
075: * @throws InvalidXmlException throws if the passed in element is not the correct
076: * element the object loads from
077: * @throws InconsistentDocElementStateException throws if blanks should not be
078: * allowed and the element passed in is blank (null)
079: */
080: public static boolean isElementPresent(String elementName,
081: Element element, boolean allowBlank)
082: throws InvalidXmlException,
083: InconsistentDocElementStateException {
084: LOG.debug("returnWithNoWorkDone elementName = " + elementName);
085:
086: if (element == null) {
087: LOG.debug("Element is null.");
088:
089: if (allowBlank) {
090: LOG.debug("Allowing blank returning true.");
091:
092: return true;
093: } else {
094: LOG
095: .debug("Not allowing blank throwing InconsistentDocElementStateException");
096: throw new InconsistentDocElementStateException(
097: "Null Element passed "
098: + "in and allowBlank set false");
099: }
100: }
101:
102: if (!elementName.equals(element.getName())) {
103: LOG
104: .debug("Element name and class' element name are inconsistent "
105: + "throwing InvalidXmlException");
106: throw new InvalidXmlException(
107: "Element is of the wrong type");
108: }
109:
110: LOG.debug("everything good, returning false");
111:
112: return false;
113: }
114: }
115:
116: /*
117: * Copyright 2003 The Trustees of Indiana University. All rights reserved.
118: *
119: * This file is part of the EDEN software package.
120: * For license information, see the LICENSE file in the top level directory
121: * of the EDEN source distribution.
122: */
|