org.odftoolkit.simple.form.AbstractFormContainer Maven / Gradle / Ivy
/*
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.odftoolkit.simple.form;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.odftoolkit.odfdom.dom.element.form.FormFormElement;
import org.odftoolkit.odfdom.dom.element.office.OfficeFormsElement;
import org.odftoolkit.odfdom.pkg.OdfElement;
import org.w3c.dom.DOMException;
/**
* AbstractFormContainer is an abstract implementation of the FormContainer
* interface, with a default implementation for every method defined in
* FormContainer, except getFormContainerElement(). A subclass must implement
* the abstract method getFormContainerElement().
*
* @since 0.8
*/
public abstract class AbstractFormContainer implements FormContainer {
private FormProvider provider = null;
public abstract OfficeFormsElement getFormContainerElement();
public Form createForm(String name) {
return getProvider().createForm(name, getFormContainerElement());
}
public boolean removeForm(Form form) {
if (form == null)
return true;
OdfElement containerElement = getFormContainerElement();
FormFormElement formElement = form.getOdfElement();
try {
containerElement.removeChild(formElement);
} catch (DOMException exception) {
Logger.getLogger(AbstractFormContainer.class.getName()).log(
Level.WARNING, exception.getMessage());
return false;
}
return true;
}
public Form getFormByName(String name) {
if (name == null)
return null;
OdfElement container = getFormContainerElement();
FormFormElement element = OdfElement.findFirstChildNode(
FormFormElement.class, container);
while (element != null) {
if (name.equals(element.getFormNameAttribute())) {
Form form = getProvider().getInstanceOf(element);
return form;
}
element = OdfElement.findNextChildNode(FormFormElement.class,
element);
}
return null;
}
public Iterator