Java tutorial
/** * Copyright (C) 2013 Seajas, the Netherlands. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3, as * published by the Free Software Foundation. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ package com.seajas.search.profiler.validator; import java.io.StringReader; import java.net.MalformedURLException; import java.net.URL; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Component; import org.springframework.validation.Errors; import org.springframework.validation.Validator; import com.seajas.search.profiler.model.command.ArchiveCommand; /** * Archive validator. * * @author Jasper van Veghel <jasper@seajas.com> */ @Component("archiveValidator") public class ArchiveValidator implements Validator { /** * Determine whether this validator supports the command object. * * @param klass * @return boolean */ @Override public boolean supports(final Class<?> klass) { return klass.equals(ArchiveCommand.class); } /** * Validate the given command object. * * @param command * @param errors */ @Override public void validate(final Object command, final Errors errors) { ArchiveCommand archive = (ArchiveCommand) command; if (archive.getAction().equals("add") || archive.getAction().equals("edit")) { if (StringUtils.isEmpty(archive.getName())) errors.rejectValue("name", "archives.error.no.name"); if (StringUtils.isEmpty(archive.getDescription())) errors.rejectValue("description", "archives.error.no.description"); if (!StringUtils.isEmpty(archive.getTransformerContent())) { StreamSource source = new StreamSource(new StringReader(archive.getTransformerContent())); try { TransformerFactory.newInstance().newTransformer(source); } catch (Exception e) { errors.rejectValue("transformerContent", "archives.error.invalid.transformation"); } } else errors.rejectValue("transformerContent", "archives.error.invalid.transformation"); if (StringUtils.isEmpty(archive.getInternalLink())) errors.rejectValue("internalLink", "archives.error.no.internal.link"); if (!StringUtils.isEmpty(archive.getDeletionExpression())) try { Pattern.compile(archive.getDeletionExpression()); } catch (PatternSyntaxException e) { errors.rejectValue("deletionExpression", "archives.error.invalid.expression"); } if (!StringUtils.isEmpty(archive.getExclusionExpression())) try { Pattern.compile(archive.getExclusionExpression()); } catch (PatternSyntaxException e) { errors.rejectValue("exclusionExpression", "archives.error.invalid.expression"); } for (String archiveUrl : archive.getArchiveUrls()) try { new URL(archiveUrl); } catch (MalformedURLException e) { errors.rejectValue("archiveUrls", "archives.error.invalid.urls"); break; } } } }