/* * Copyright (c) 2013, the authors. * * This file is part of 'DXFS'. * * DXFS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * DXFS 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 DXFS. If not, see <http://www.gnu.org/licenses/>. */ package nextflow.fs.dx; import java.io.IOException; import java.util.Map; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; /** * Utility methods * * @author Paolo Di Tommaso <paolo.ditommaso@gmail.com> * */ public class DxHelper { static private ObjectMapper mapper = new ObjectMapper(); /** * Verify if the specified string is a DnaNexus file identifier * * @param str The file string id * @return {@code true} when the string matches the file id syntax */ static boolean isFileId( String str ) { if( str == null ) return false; return DxFileSystemProvider.FILE_ID_PATTERN.matcher(str).matches(); } /** * Whenever the specified string matches the project id or the container id syntax * * @param str * @return */ static boolean isContextId( String str ) { if( str == null ) return false; return DxFileSystemProvider.CONTEXT_ID_PATTERN.matcher(str).matches(); } /** * Parse Json formatted string to a {@code JsonNode} object instance * * @param str The source JSON string * @return The resulting {@code JsonNode} */ static JsonNode parseJsonString( String str ) { try { return mapper.readValue(str, JsonNode.class); } catch( IOException e ) { throw new IllegalArgumentException(String.format("Error parsing json string:\n%s", str), e); } } /** * Converts a generic Java object to a {@code JsoNode} object * @param obj The source Java object, usually it is a {@code Map} * @return The mapped {@code JsonNode} instance */ static public JsonNode objToJson(Object obj) { return mapper.valueToTree(obj); } /** * Converts a {@code JsonNode} to a generic Java {@code Map} object * * @param node The source Json node * @return The resulting Map object */ static public Map jsonToObj(JsonNode node) { return mapper.convertValue(node, Map.class); } }