Java tutorial
/* Copyright 2010 - 2014 by Brian Uri! This file is part of DDMSence. This library is free software; you can redistribute it and/or modify it under the terms of version 3.0 of the GNU Lesser General Public License as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with DDMSence. If not, see <http://www.gnu.org/licenses/>. You can contact the author at ddmsence@urizone.net. The DDMSence home page is located at http://ddmsence.urizone.net/ */ package buri.ddmsence.ddms.format; import nu.xom.Element; import buri.ddmsence.AbstractBaseComponent; import buri.ddmsence.AbstractQualifierValue; import buri.ddmsence.ddms.IBuilder; import buri.ddmsence.ddms.InvalidDDMSException; import buri.ddmsence.ddms.OutputFormat; import buri.ddmsence.util.DDMSVersion; import buri.ddmsence.util.Util; import com.google.gson.JsonObject; /** * An immutable implementation of ddms:extent. * <br /><br /> * {@ddms.versions 11111} * * <p></p> * * {@table.header History} * None. * {@table.footer} * {@table.header Nested Elements} * None. * {@table.footer} * {@table.header Attributes} * {@child.info ddms:qualifier|0..1|11111} * {@child.info ddms:value|0..1|11111} * {@table.footer} * {@table.header Validation Rules} * {@ddms.rule The qualified name of this element must be correct.|Error|11111} * {@ddms.rule ddms:qualifier must exist if ddms:value is set.|Error|11111} * {@ddms.rule If set, the ddms:qualifier must be a valid URI.|Error|11111} * {@ddms.rule A ddms:qualifier can be set with no ddms:value.|Warning|11111} * {@ddms.rule This component can be used with no values set.|Warning|11111} * <p>Note: The ddms:value is not validated against the qualifier's vocabulary.</p> * {@table.footer} * * @author Brian Uri! * @since 0.9.b */ public final class Extent extends AbstractQualifierValue { /** * Constructor for creating a component from a XOM Element * * @param element the XOM element representing this * @throws InvalidDDMSException if any required information is missing or malformed */ public Extent(Element element) throws InvalidDDMSException { super(element, true); } /** * Constructor for creating a component from raw data * * @param qualifier the value of the qualifier attribute * @param value the value of the value attribute * @throws InvalidDDMSException if any required information is missing or malformed */ public Extent(String qualifier, String value) throws InvalidDDMSException { super(Extent.getName(DDMSVersion.getCurrentVersion()), qualifier, value, true, true); } /** * Validates the component. * * @see AbstractBaseComponent#validate() * @throws InvalidDDMSException if any required information is missing or malformed */ protected void validate() throws InvalidDDMSException { Util.requireDDMSQName(getXOMElement(), Extent.getName(getDDMSVersion())); if (!Util.isEmpty(getValue())) Util.requireDDMSValue("qualifier attribute", getQualifier()); if (!Util.isEmpty(getQualifier())) Util.requireDDMSValidURI(getQualifier()); super.validate(); } /** * @see AbstractBaseComponent#validateWarnings() */ protected void validateWarnings() { if (!Util.isEmpty(getQualifier()) && Util.isEmpty(getValue())) addWarning("A qualifier has been set without an accompanying value attribute."); if (Util.isEmpty(getQualifier()) && Util.isEmpty(getValue())) addWarning("A completely empty ddms:extent element was found."); super.validateWarnings(); } /** * @see AbstractBaseComponent#getJSONObject() */ public JsonObject getJSONObject() { JsonObject object = new JsonObject(); addJson(object, getQualifierName(), getQualifier()); addJson(object, getValueName(), getValue()); return (object); } /** * @see AbstractBaseComponent#getHTMLTextOutput(OutputFormat, String, String) */ public String getHTMLTextOutput(OutputFormat format, String prefix, String suffix) { Util.requireHTMLText(format); String localPrefix = buildPrefix(prefix, getName(), suffix + "."); StringBuffer text = new StringBuffer(); text.append(buildHTMLTextOutput(format, localPrefix + getQualifierName(), getQualifier())); text.append(buildHTMLTextOutput(format, localPrefix + getValueName(), getValue())); return (text.toString()); } /** * @see Object#equals(Object) */ public boolean equals(Object obj) { return (super.equals(obj) && (obj instanceof Extent)); } /** * Accessor for the element name of this component, based on the version of DDMS used * * @param version the DDMSVersion * @return an element name */ public static String getName(DDMSVersion version) { Util.requireValue("version", version); return ("extent"); } /** * Builder for this DDMS component. * * @see IBuilder * @author Brian Uri! * @since 1.8.0 */ public static class Builder extends AbstractQualifierValue.Builder { private static final long serialVersionUID = -5658005476173591056L; /** * Empty constructor */ public Builder() { super(); } /** * Constructor which starts from an existing component. */ public Builder(Extent mediaExtent) { super(mediaExtent); } /** * @see IBuilder#commit() */ public Extent commit() throws InvalidDDMSException { return (isEmpty() ? null : new Extent(getQualifier(), getValue())); } /** * @see IBuilder#isEmpty() */ public boolean isEmpty() { return (Util.isEmpty(getQualifier()) && Util.isEmpty(getValue())); } } }