Modified: incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/ClassExistsCmdLineOptionValidator.java URL: http://svn.apache.org/viewvc/incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/ClassExistsCmdLineOptionValidator.java?rev=917722&r1=917721&r2=917722&view=diff ============================================================================== --- incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/ClassExistsCmdLineOptionValidator.java (original) +++ incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/ClassExistsCmdLineOptionValidator.java Mon Mar 1 21:17:24 2010 @@ -1,38 +1,51 @@ -//Copyright (c) 2008, California Institute of Technology. -//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. -// -//$Id$ - -package gov.nasa.jpl.oodt.cas.commons.option.validator; - -//JDK imports -import java.util.logging.Level; - -//OODT imports -import gov.nasa.jpl.oodt.cas.commons.option.CmdLineOptionInstance; - -/** - * - * @author bfoster - * @version $Revision$ - * - * <p>Describe your class here</p>. - */ -public class ClassExistsCmdLineOptionValidator implements - CmdLineOptionValidator { - - public boolean validate(CmdLineOptionInstance optionInst) { - for (String value : optionInst.getValues()) { - try { - Class.forName(value); - } catch (Exception e) { - LOG.log(Level.SEVERE, "Option value " + value + " for option " - + optionInst.getOption().getId() - + " is not a valid class"); - return false; - } - } - return true; - } - -} +/* + * 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 gov.nasa.jpl.oodt.cas.commons.option.validator; + +//JDK imports +import java.util.logging.Level; + +//OODT imports +import gov.nasa.jpl.oodt.cas.commons.option.CmdLineOptionInstance; + +/** + * + * @author bfoster + * @version $Revision$ + * + * <p>Describe your class here</p>. + */ +public class ClassExistsCmdLineOptionValidator implements + CmdLineOptionValidator { + + public boolean validate(CmdLineOptionInstance optionInst) { + for (String value : optionInst.getValues()) { + try { + Class.forName(value); + } catch (Exception e) { + LOG.log(Level.SEVERE, "Option value " + value + " for option " + + optionInst.getOption().getId() + + " is not a valid class"); + return false; + } + } + return true; + } + +}
Modified: incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/CmdLineOptionValidator.java URL: http://svn.apache.org/viewvc/incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/CmdLineOptionValidator.java?rev=917722&r1=917721&r2=917722&view=diff ============================================================================== --- incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/CmdLineOptionValidator.java (original) +++ incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/CmdLineOptionValidator.java Mon Mar 1 21:17:24 2010 @@ -1,28 +1,41 @@ -//Copyright (c) 2008, California Institute of Technology. -//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. -// -//$Id$ - -package gov.nasa.jpl.oodt.cas.commons.option.validator; - -//JDK imports -import java.util.logging.Logger; - -//OODT imports -import gov.nasa.jpl.oodt.cas.commons.option.CmdLineOptionInstance; - -/** - * - * @author bfoster - * @version $Revision$ - * - * <p>Describe your class here</p>. - */ -public interface CmdLineOptionValidator { - - static Logger LOG = Logger - .getLogger(CmdLineOptionValidator.class.getName()); - - public boolean validate(CmdLineOptionInstance optionInst); - -} \ No newline at end of file +/* + * 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 gov.nasa.jpl.oodt.cas.commons.option.validator; + +//JDK imports +import java.util.logging.Logger; + +//OODT imports +import gov.nasa.jpl.oodt.cas.commons.option.CmdLineOptionInstance; + +/** + * + * @author bfoster + * @version $Revision$ + * + * <p>Describe your class here</p>. + */ +public interface CmdLineOptionValidator { + + static Logger LOG = Logger + .getLogger(CmdLineOptionValidator.class.getName()); + + public boolean validate(CmdLineOptionInstance optionInst); + +} Modified: incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/FileExistCmdLineOptionValidator.java URL: http://svn.apache.org/viewvc/incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/FileExistCmdLineOptionValidator.java?rev=917722&r1=917721&r2=917722&view=diff ============================================================================== --- incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/FileExistCmdLineOptionValidator.java (original) +++ incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/FileExistCmdLineOptionValidator.java Mon Mar 1 21:17:24 2010 @@ -1,36 +1,49 @@ -//Copyright (c) 2008, California Institute of Technology. -//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. -// -//$Id$ - -package gov.nasa.jpl.oodt.cas.commons.option.validator; - -//JDK imports -import java.io.File; -import java.util.logging.Level; - -//OODT imports -import gov.nasa.jpl.oodt.cas.commons.option.CmdLineOptionInstance; - -/** - * - * @author bfoster - * @version $Revision$ - * - * <p>Describe your class here</p>. - */ -public class FileExistCmdLineOptionValidator implements CmdLineOptionValidator { - - public boolean validate(CmdLineOptionInstance optionInst) { - for (String value : optionInst.getValues()) { - if (!new File(value).exists()) { - LOG.log(Level.SEVERE, "Option value " + value + " for option " - + optionInst.getOption().getId() - + " is not an existing file"); - return false; - } - } - return true; - } - -} +/* + * 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 gov.nasa.jpl.oodt.cas.commons.option.validator; + +//JDK imports +import java.io.File; +import java.util.logging.Level; + +//OODT imports +import gov.nasa.jpl.oodt.cas.commons.option.CmdLineOptionInstance; + +/** + * + * @author bfoster + * @version $Revision$ + * + * <p>Describe your class here</p>. + */ +public class FileExistCmdLineOptionValidator implements CmdLineOptionValidator { + + public boolean validate(CmdLineOptionInstance optionInst) { + for (String value : optionInst.getValues()) { + if (!new File(value).exists()) { + LOG.log(Level.SEVERE, "Option value " + value + " for option " + + optionInst.getOption().getId() + + " is not an existing file"); + return false; + } + } + return true; + } + +} Modified: incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/NoRestrictionsCmdLineOptionValidator.java URL: http://svn.apache.org/viewvc/incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/NoRestrictionsCmdLineOptionValidator.java?rev=917722&r1=917721&r2=917722&view=diff ============================================================================== --- incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/NoRestrictionsCmdLineOptionValidator.java (original) +++ incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/option/validator/NoRestrictionsCmdLineOptionValidator.java Mon Mar 1 21:17:24 2010 @@ -1,25 +1,38 @@ -//Copyright (c) 2008, California Institute of Technology. -//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. -// -//$Id$ - -package gov.nasa.jpl.oodt.cas.commons.option.validator; - -//OODT imports -import gov.nasa.jpl.oodt.cas.commons.option.CmdLineOptionInstance; - -/** - * - * @author bfoster - * @version $Revision$ - * - * <p>Describe your class here</p>. - */ -public class NoRestrictionsCmdLineOptionValidator implements - CmdLineOptionValidator { - - public boolean validate(CmdLineOptionInstance optionInst) { - return true; - } - -} +/* + * 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 gov.nasa.jpl.oodt.cas.commons.option.validator; + +//OODT imports +import gov.nasa.jpl.oodt.cas.commons.option.CmdLineOptionInstance; + +/** + * + * @author bfoster + * @version $Revision$ + * + * <p>Describe your class here</p>. + */ +public class NoRestrictionsCmdLineOptionValidator implements + CmdLineOptionValidator { + + public boolean validate(CmdLineOptionInstance optionInst) { + return true; + } + +} Modified: incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/pagination/PaginationUtils.java URL: http://svn.apache.org/viewvc/incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/pagination/PaginationUtils.java?rev=917722&r1=917721&r2=917722&view=diff ============================================================================== --- incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/pagination/PaginationUtils.java (original) +++ incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/pagination/PaginationUtils.java Mon Mar 1 21:17:24 2010 @@ -1,92 +1,105 @@ -//Copyright (c) 2006, California Institute of Technology. -//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. -// -//$Id$ - -package gov.nasa.jpl.oodt.cas.commons.pagination; - -import java.util.List; - -/** - * @author mattmann - * @version $Revision$ - * - * <p> - * A set of utility methods to do pagination. - * </p> - * - */ -public final class PaginationUtils { - - private PaginationUtils() throws InstantiationException { - throw new InstantiationException("Don't construct utility classes!"); - } - - public static int hasRemainder(int divisor, int quotient) { - if (divisor % quotient != 0) { - return 1; - } else { - return 0; - } - } - - public static int computeCurrentPage(int startIdx, int productsPerPage) { - return ((startIdx + 1) / productsPerPage) - + hasRemainder(startIdx + 1, productsPerPage); - } - - public static int computeCurrentWindow(int currPage, int pagesPerPage) { - return (currPage / pagesPerPage) + hasRemainder(currPage, pagesPerPage); - } - - public static int computeMinPage(int pagesPerPage, int currWindow) { - return (pagesPerPage * (currWindow - 1)) + 1; - } - - public static int computeMaxPage(int minPage, int pagesPerPage, - int lastIdx, int productsPerPage) { - return Math.min(minPage + (pagesPerPage - 1), computeCurrentPage( - lastIdx, productsPerPage)); - } - - public static int computeEndIdx(int currPage, int productsPerPage, - int totalProducts) { - return Math.min((currPage * productsPerPage) - 1, totalProducts - 1); - } - - public static int computePrevStartIdx(int minPage, int productsPerPage) { - int prevPage = Math.max(minPage - 2, 1); - return (prevPage * productsPerPage); - } - - public static int computeNextStartIdx(int maxPage, int productsPerPage) { - int nextPage = maxPage; - return (nextPage * productsPerPage); - - } - - public static List iterateFrom(final int startIndex, - final List originalList, int pageSize) { - final int totalSize = originalList.size(); - - int endIndex = startIndex + pageSize; - if (endIndex > totalSize) - endIndex = totalSize; - - return originalList.subList(startIndex, endIndex); - } - - public static int getTotalPage(List originalList, int pageSize) { - if (originalList == null || originalList.size() <= 0) - return 0; - final int totalSize = originalList.size(); - return ((totalSize - 1) / pageSize) + 1; - } - - public static int getTotalPage(int numTotal, int pageSize) { - if (numTotal <= 0) - return 0; - return ((numTotal - 1) / pageSize) + 1; - } - -} +/* + * 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 gov.nasa.jpl.oodt.cas.commons.pagination; + +import java.util.List; + +/** + * @author mattmann + * @version $Revision$ + * + * <p> + * A set of utility methods to do pagination. + * </p> + * + */ +public final class PaginationUtils { + + private PaginationUtils() throws InstantiationException { + throw new InstantiationException("Don't construct utility classes!"); + } + + public static int hasRemainder(int divisor, int quotient) { + if (divisor % quotient != 0) { + return 1; + } else { + return 0; + } + } + + public static int computeCurrentPage(int startIdx, int productsPerPage) { + return ((startIdx + 1) / productsPerPage) + + hasRemainder(startIdx + 1, productsPerPage); + } + + public static int computeCurrentWindow(int currPage, int pagesPerPage) { + return (currPage / pagesPerPage) + hasRemainder(currPage, pagesPerPage); + } + + public static int computeMinPage(int pagesPerPage, int currWindow) { + return (pagesPerPage * (currWindow - 1)) + 1; + } + + public static int computeMaxPage(int minPage, int pagesPerPage, + int lastIdx, int productsPerPage) { + return Math.min(minPage + (pagesPerPage - 1), computeCurrentPage( + lastIdx, productsPerPage)); + } + + public static int computeEndIdx(int currPage, int productsPerPage, + int totalProducts) { + return Math.min((currPage * productsPerPage) - 1, totalProducts - 1); + } + + public static int computePrevStartIdx(int minPage, int productsPerPage) { + int prevPage = Math.max(minPage - 2, 1); + return (prevPage * productsPerPage); + } + + public static int computeNextStartIdx(int maxPage, int productsPerPage) { + int nextPage = maxPage; + return (nextPage * productsPerPage); + + } + + public static List iterateFrom(final int startIndex, + final List originalList, int pageSize) { + final int totalSize = originalList.size(); + + int endIndex = startIndex + pageSize; + if (endIndex > totalSize) + endIndex = totalSize; + + return originalList.subList(startIndex, endIndex); + } + + public static int getTotalPage(List originalList, int pageSize) { + if (originalList == null || originalList.size() <= 0) + return 0; + final int totalSize = originalList.size(); + return ((totalSize - 1) / pageSize) + 1; + } + + public static int getTotalPage(int numTotal, int pageSize) { + if (numTotal <= 0) + return 0; + return ((numTotal - 1) / pageSize) + 1; + } + +} Modified: incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/spring/SpringSetIdInjectionType.java URL: http://svn.apache.org/viewvc/incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/spring/SpringSetIdInjectionType.java?rev=917722&r1=917721&r2=917722&view=diff ============================================================================== --- incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/spring/SpringSetIdInjectionType.java (original) +++ incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/spring/SpringSetIdInjectionType.java Mon Mar 1 21:17:24 2010 @@ -1,34 +1,47 @@ -//Copyright (c) 2008, California Institute of Technology. -//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. -// -//$Id$ - -package gov.nasa.jpl.oodt.cas.commons.spring; - -//Spring imports -import org.springframework.beans.factory.annotation.Required; - -/** - * - * @author bfoster - * @version $Revision$ - * - * <p>Describe your class here</p>. - */ -public interface SpringSetIdInjectionType { - - /** - * Get a unique String id that represents this objects instance - * @return Unique String id - */ - @Required - public String getId(); - - /** - * Set a unique id that represents this objects instance - * @param id Unique String id - */ - @Required - public void setId(String id); - -} +/* + * 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 gov.nasa.jpl.oodt.cas.commons.spring; + +//Spring imports +import org.springframework.beans.factory.annotation.Required; + +/** + * + * @author bfoster + * @version $Revision$ + * + * <p>Describe your class here</p>. + */ +public interface SpringSetIdInjectionType { + + /** + * Get a unique String id that represents this objects instance + * @return Unique String id + */ + @Required + public String getId(); + + /** + * Set a unique id that represents this objects instance + * @param id Unique String id + */ + @Required + public void setId(String id); + +} Modified: incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/spring/postprocessor/SetIdBeanPostProcessor.java URL: http://svn.apache.org/viewvc/incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/spring/postprocessor/SetIdBeanPostProcessor.java?rev=917722&r1=917721&r2=917722&view=diff ============================================================================== --- incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/spring/postprocessor/SetIdBeanPostProcessor.java (original) +++ incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/spring/postprocessor/SetIdBeanPostProcessor.java Mon Mar 1 21:17:24 2010 @@ -1,36 +1,49 @@ -//Copyright (c) 2008, California Institute of Technology. -//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. -// -//$Id$ - -package gov.nasa.jpl.oodt.cas.commons.spring.postprocessor; - -//OODT imports -import gov.nasa.jpl.oodt.cas.commons.spring.SpringSetIdInjectionType; - -//Spring imports -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.config.BeanPostProcessor; - -/** - * - * @author bfoster - * @version $Revision$ - * - * <p>Describe your class here</p>. - */ -public class SetIdBeanPostProcessor implements BeanPostProcessor { - - public Object postProcessAfterInitialization(Object bean, String beanName) - throws BeansException { - if (bean instanceof SpringSetIdInjectionType) - ((SpringSetIdInjectionType) bean).setId(beanName); - return bean; - } - - public Object postProcessBeforeInitialization(Object bean, String beanName) - throws BeansException { - return bean; - } - -} +/* + * 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 gov.nasa.jpl.oodt.cas.commons.spring.postprocessor; + +//OODT imports +import gov.nasa.jpl.oodt.cas.commons.spring.SpringSetIdInjectionType; + +//Spring imports +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.config.BeanPostProcessor; + +/** + * + * @author bfoster + * @version $Revision$ + * + * <p>Describe your class here</p>. + */ +public class SetIdBeanPostProcessor implements BeanPostProcessor { + + public Object postProcessAfterInitialization(Object bean, String beanName) + throws BeansException { + if (bean instanceof SpringSetIdInjectionType) + ((SpringSetIdInjectionType) bean).setId(beanName); + return bean; + } + + public Object postProcessBeforeInitialization(Object bean, String beanName) + throws BeansException { + return bean; + } + +} Modified: incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/xml/DOMUtil.java URL: http://svn.apache.org/viewvc/incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/xml/DOMUtil.java?rev=917722&r1=917721&r2=917722&view=diff ============================================================================== --- incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/xml/DOMUtil.java (original) +++ incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/xml/DOMUtil.java Mon Mar 1 21:17:24 2010 @@ -1,7 +1,20 @@ -//Copyright (c) 2005, California Institute of Technology. -//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. -// -//$Id$ +/* + * 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 gov.nasa.jpl.oodt.cas.commons.xml; Modified: incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/xml/XMLUtils.java URL: http://svn.apache.org/viewvc/incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/xml/XMLUtils.java?rev=917722&r1=917721&r2=917722&view=diff ============================================================================== --- incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/xml/XMLUtils.java (original) +++ incubator/oodt/cas-commons/trunk/src/main/java/gov/nasa/jpl/oodt/cas/commons/xml/XMLUtils.java Mon Mar 1 21:17:24 2010 @@ -1,221 +1,234 @@ -//Copyright (c) 2005, California Institute of Technology. -//ALL RIGHTS RESERVED. U.S. Government sponsorship acknowledged. -// -//$Id$ - -package gov.nasa.jpl.oodt.cas.commons.xml; - -//JDK imports -import javax.xml.parsers.DocumentBuilder; -import javax.xml.parsers.DocumentBuilderFactory; -import javax.xml.transform.Result; -import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.stream.StreamResult; -import javax.xml.transform.dom.DOMSource; -import javax.xml.transform.OutputKeys; -import org.w3c.dom.Attr; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; -import org.w3c.dom.Text; -import org.xml.sax.InputSource; -import java.io.InputStream; -import java.io.OutputStream; -import java.net.URLDecoder; -import java.util.List; -import java.util.Map; -import java.util.Vector; -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * @author mattmann - * @author bfoster - * @version $Revision$ - * - * - * <p> - * A Utility class containing methods to write and transform XML objects. - * </p> - */ - -public class XMLUtils { - - /* our log stream */ - private final static Logger LOG = Logger - .getLogger(XMLUtils.class.getName()); - - /** - * <p> - * This method writes a DOM document to a file - * </p>. - * - * @param doc - * The DOM document to write. - * @param filename - * The filename to write the DOM document to. - */ - public static void writeXmlFile(Document doc, String filename) { - // Prepare the output file - Result result = new StreamResult(filename); - transform(doc, result); - } - - public static void writeXmlToStream(Document doc, OutputStream stream) { - Result result = new StreamResult(stream); - transform(doc, result); - } - - private static void transform(Document doc, Result result) { - try { - // Prepare the DOM document for writing - Source source = new DOMSource(doc); - - // Write the DOM document to the file - Transformer xformer = TransformerFactory.newInstance() - .newTransformer(); - xformer.setOutputProperty(OutputKeys.INDENT, "yes"); - xformer.transform(source, result); - } catch (Exception e) { - e.printStackTrace(); - } - - } - - public static List readMany(Element root, String elt) { - return readMany(root, elt, "UTF-8"); - } - - public static List readMany(Element root, String elt, String encoding) { - NodeList valueNodes = root.getElementsByTagName(elt); - List values = new Vector(); - - for (int i = 0; i < valueNodes.getLength(); i++) { - Element valElem = (Element) valueNodes.item(i); - String value = null; - - try { - value = URLDecoder.decode( - DOMUtil.getSimpleElementText(valElem), encoding); - values.add(value); - } catch (Exception e) { - e.printStackTrace(); - LOG.log(Level.WARNING, "Error decoding tag: [" + elt - + "]: val: [" + DOMUtil.getSimpleElementText(valElem) - + "] from metadata. Message: " + e.getMessage()); - } - } - - return values; - } - - public static String read(Element root, String elt) { - return read(root, elt, "UTF-8"); - } - - public static String read(Element root, String elt, String encoding) { - - String value = null; - try { - value = URLDecoder.decode(DOMUtil.getSimpleElementText(root, elt), - encoding); - } catch (Exception e) { - LOG.log(Level.WARNING, "Error decoding " + elt + "from metadata. " - + "Message: " + e.getMessage()); - } - return value; - } - - public static Element getFirstElement(String name, Element root) { - NodeList list = root.getElementsByTagName(name); - if (list != null) { - return (Element) list.item(0); - } else - return null; - } - - public static String getSimpleElementText(Element node, boolean trim) { - if (node.getChildNodes().item(0) instanceof Text) { - String elemTxt = null; - if (trim) { - elemTxt = node.getChildNodes().item(0).getNodeValue().trim(); - } else { - elemTxt = node.getChildNodes().item(0).getNodeValue(); - } - - return elemTxt; - } else - return null; - } - - public static String getSimpleElementText(Element node) { - return getSimpleElementText(node, false); - } - - public static String getElementText(String elemName, Element root, - boolean trim) { - Element elem = getFirstElement(elemName, root); - if (elem != null) { - return getSimpleElementText(elem, trim); - } else - return null; - } - - public static String getElementText(String elemName, Element root) { - return getElementText(elemName, root, false); - } - - public static Document getDocumentRoot(InputStream is) { - // open up the XML file - DocumentBuilderFactory factory = null; - DocumentBuilder parser = null; - Document document = null; - InputSource inputSource = null; - - inputSource = new InputSource(is); - - try { - factory = DocumentBuilderFactory.newInstance(); - parser = factory.newDocumentBuilder(); - document = parser.parse(inputSource); - } catch (Exception e) { - LOG.log(Level.WARNING, "Unable to parse xml stream" - + ": Reason is [" + e + "]"); - return null; - } - - return document; - } - - public static Element addNode(Document doc, Node parent, String name) { - Element child = doc.createElement(name); - parent.appendChild(child); - return child; - } - - public static void addNode(Document doc, Node parent, String name, - String text) { - Element child = doc.createElement(name); - child.appendChild(doc.createTextNode(text)); - parent.appendChild(child); - } - - public static void addNode(Document doc, Node parent, String ns, - String name, String text, Map NS_MAP) { - Element child = doc.createElementNS((String) NS_MAP.get(ns), ns + ":" - + name); - child.appendChild(doc.createTextNode(text)); - parent.appendChild(child); - } - - public static void addAttribute(Document doc, Element node, String name, - String value) { - Attr attribute = doc.createAttribute(name); - attribute.setValue(value); - node.getAttributes().setNamedItem(attribute); - } - -} +/* + * 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 gov.nasa.jpl.oodt.cas.commons.xml; + +//JDK imports +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.transform.Result; +import javax.xml.transform.Source; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.OutputKeys; +import org.w3c.dom.Attr; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.w3c.dom.Text; +import org.xml.sax.InputSource; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.URLDecoder; +import java.util.List; +import java.util.Map; +import java.util.Vector; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * @author mattmann + * @author bfoster + * @version $Revision$ + * + * + * <p> + * A Utility class containing methods to write and transform XML objects. + * </p> + */ + +public class XMLUtils { + + /* our log stream */ + private final static Logger LOG = Logger + .getLogger(XMLUtils.class.getName()); + + /** + * <p> + * This method writes a DOM document to a file + * </p>. + * + * @param doc + * The DOM document to write. + * @param filename + * The filename to write the DOM document to. + */ + public static void writeXmlFile(Document doc, String filename) { + // Prepare the output file + Result result = new StreamResult(filename); + transform(doc, result); + } + + public static void writeXmlToStream(Document doc, OutputStream stream) { + Result result = new StreamResult(stream); + transform(doc, result); + } + + private static void transform(Document doc, Result result) { + try { + // Prepare the DOM document for writing + Source source = new DOMSource(doc); + + // Write the DOM document to the file + Transformer xformer = TransformerFactory.newInstance() + .newTransformer(); + xformer.setOutputProperty(OutputKeys.INDENT, "yes"); + xformer.transform(source, result); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + public static List readMany(Element root, String elt) { + return readMany(root, elt, "UTF-8"); + } + + public static List readMany(Element root, String elt, String encoding) { + NodeList valueNodes = root.getElementsByTagName(elt); + List values = new Vector(); + + for (int i = 0; i < valueNodes.getLength(); i++) { + Element valElem = (Element) valueNodes.item(i); + String value = null; + + try { + value = URLDecoder.decode( + DOMUtil.getSimpleElementText(valElem), encoding); + values.add(value); + } catch (Exception e) { + e.printStackTrace(); + LOG.log(Level.WARNING, "Error decoding tag: [" + elt + + "]: val: [" + DOMUtil.getSimpleElementText(valElem) + + "] from metadata. Message: " + e.getMessage()); + } + } + + return values; + } + + public static String read(Element root, String elt) { + return read(root, elt, "UTF-8"); + } + + public static String read(Element root, String elt, String encoding) { + + String value = null; + try { + value = URLDecoder.decode(DOMUtil.getSimpleElementText(root, elt), + encoding); + } catch (Exception e) { + LOG.log(Level.WARNING, "Error decoding " + elt + "from metadata. " + + "Message: " + e.getMessage()); + } + return value; + } + + public static Element getFirstElement(String name, Element root) { + NodeList list = root.getElementsByTagName(name); + if (list != null) { + return (Element) list.item(0); + } else + return null; + } + + public static String getSimpleElementText(Element node, boolean trim) { + if (node.getChildNodes().item(0) instanceof Text) { + String elemTxt = null; + if (trim) { + elemTxt = node.getChildNodes().item(0).getNodeValue().trim(); + } else { + elemTxt = node.getChildNodes().item(0).getNodeValue(); + } + + return elemTxt; + } else + return null; + } + + public static String getSimpleElementText(Element node) { + return getSimpleElementText(node, false); + } + + public static String getElementText(String elemName, Element root, + boolean trim) { + Element elem = getFirstElement(elemName, root); + if (elem != null) { + return getSimpleElementText(elem, trim); + } else + return null; + } + + public static String getElementText(String elemName, Element root) { + return getElementText(elemName, root, false); + } + + public static Document getDocumentRoot(InputStream is) { + // open up the XML file + DocumentBuilderFactory factory = null; + DocumentBuilder parser = null; + Document document = null; + InputSource inputSource = null; + + inputSource = new InputSource(is); + + try { + factory = DocumentBuilderFactory.newInstance(); + parser = factory.newDocumentBuilder(); + document = parser.parse(inputSource); + } catch (Exception e) { + LOG.log(Level.WARNING, "Unable to parse xml stream" + + ": Reason is [" + e + "]"); + return null; + } + + return document; + } + + public static Element addNode(Document doc, Node parent, String name) { + Element child = doc.createElement(name); + parent.appendChild(child); + return child; + } + + public static void addNode(Document doc, Node parent, String name, + String text) { + Element child = doc.createElement(name); + child.appendChild(doc.createTextNode(text)); + parent.appendChild(child); + } + + public static void addNode(Document doc, Node parent, String ns, + String name, String text, Map NS_MAP) { + Element child = doc.createElementNS((String) NS_MAP.get(ns), ns + ":" + + name); + child.appendChild(doc.createTextNode(text)); + parent.appendChild(child); + } + + public static void addAttribute(Document doc, Element node, String name, + String value) { + Attr attribute = doc.createAttribute(name); + attribute.setValue(value); + node.getAttributes().setNamedItem(attribute); + } + +}
