Java tutorial
/* * Copyright 2009 GBIF. Licensed 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 net.sibcolombia.sibsp.model.factory; import java.net.URI; import com.google.inject.Inject; import net.sibcolombia.sibsp.model.ExtensionProperty; import net.sibcolombia.sibsp.model.Vocabulary; import net.sibcolombia.sibsp.service.admin.VocabulariesManager; import org.apache.commons.digester.Rule; import org.apache.log4j.Logger; import org.xml.sax.Attributes; /** * This will call the root of the stack to find the url2thesaurus, and then set the appropriate thesaurus on the * extension. Namespaces are completely ignored. The "thesaurus" attribute is searched for and if found, the thesaurus * is set if found. */ public class ThesaurusHandlingRule extends Rule { public static final String ATTRIBUTE_THESAURUS = "thesaurus"; private static final Logger LOG = Logger.getLogger(ThesaurusHandlingRule.class); private final VocabulariesManager vocabManager; @Inject public ThesaurusHandlingRule(VocabulariesManager vocabManager) { this.vocabManager = vocabManager; } @Override public void begin(String namespace, String name, Attributes attributes) throws Exception { for (int i = 0; i < attributes.getLength(); i++) { if (ThesaurusHandlingRule.ATTRIBUTE_THESAURUS.equals(attributes.getQName(i))) { Vocabulary tv = null; try { URI vocabURIObject = new URI(attributes.getValue(i)); tv = vocabManager.get(vocabURIObject); } catch (Exception e) { LOG.error("Vocabulary with location " + attributes.getValue(i) + " couldnt get hold of: " + e.getMessage(), e); } if (tv == null) { LOG.warn("No Vocabulary object exists for the URL[" + attributes.getValue(i) + "] so cannot be set"); } else { Object extensionPropertyAsObject = getDigester().peek(); if (extensionPropertyAsObject instanceof ExtensionProperty) { ExtensionProperty eProperty = (ExtensionProperty) extensionPropertyAsObject; eProperty.setVocabulary(tv); LOG.debug("Vocabulary with URI[" + tv.getUriString() + "] added to extension property"); } } break; // since we found the attribute } } } }