1 /* 2 * This file is part of Healpix Java. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 * 18 */ 19 package healpix.essentials; 20 21 import java.io.*; 22 23 /** Moc string I/O routines. 24 @copyright 2014-2015 Max-Planck-Society 25 @author Martin Reinecke */ 26 public class MocStringIO 27 { 28 /** Parses a string following either the basic ASCII or JSON syntax given in 29 the MOC standard document, and converts it into a MOC. 30 The string need not obey the rules for a well-formed MOC given in the 31 document. */ mocFromString(String in)32 public static Moc mocFromString(String in) 33 { 34 in=in.replaceAll(",+"," "); // replace commas with spaces 35 in=in.replaceAll("[\\[\\]{}\"]+",""); // get rid of useless characters 36 in=in.replaceAll("\\s*[:/]\\s*","/ "); // stick order indicator to order 37 in=in.replaceAll("\\s*-\\s*","-"); // fuse ranges into one token 38 String[] tok = in.split("[\\s]+"); // split at whitespace 39 RangeSet ru = new RangeSet(); 40 int order=0; 41 long ofs=0; 42 for (int i=0; i<tok.length; ++i) 43 { 44 if (tok[i].isEmpty()) continue; 45 if (tok[i].contains("/")) // new order 46 { 47 tok[i]=tok[i].split("/")[0]; 48 order=Integer.parseInt(tok[i]); 49 ofs = 4*(1L<<(2*order)); 50 } 51 else if (tok[i].contains("-")) // range of values 52 { 53 String [] lim=tok[i].split("-"); 54 ru.add(Long.parseLong(lim[0])+ofs, 55 Long.parseLong(lim[1])+ofs+1); 56 } 57 else // single value 58 ru.add(Long.parseLong(tok[i])+ofs); 59 } 60 return Moc.fromUniqRS(ru); 61 } mocToStringGeneral(Moc moc, boolean json)62 private static String mocToStringGeneral(Moc moc, boolean json) 63 { 64 RangeSet ru = moc.toUniqRS(); 65 StringBuilder s = new StringBuilder(); 66 if (json) s.append("{"); 67 boolean firstOrder=true; 68 int omax = moc.maxOrder(); 69 for (int o=0; o<=omax; ++o) 70 { 71 RangeSet rt = new RangeSet(); 72 long offset=4*(1L<<(2*o)); 73 rt.append(offset, 4*offset); 74 rt=rt.intersection(ru); 75 boolean prefix=false; 76 if (!rt.isEmpty()) 77 { 78 for (int iv=0; iv<rt.nranges(); ++iv) 79 { 80 long a=rt.ivbegin(iv)-offset, 81 b=rt.ivend(iv)-offset; 82 if (!prefix) 83 { 84 if (!firstOrder) s.append(json ? ", " : " "); 85 firstOrder=false; 86 if (json) 87 s.append("\"").append(o).append("\":["); 88 else 89 s.append(o).append("/"); 90 prefix=true; 91 } 92 else 93 s.append(","); 94 if (json) 95 { 96 for (long i=a;i<b-1;++i) 97 s.append(i).append(","); 98 s.append(b-1); 99 } 100 else 101 { 102 s.append(a); 103 if (b>a+1) s.append("-").append(b-1); 104 } 105 } 106 } 107 if (json&&prefix) 108 s.append("]"); 109 } 110 if (json) s.append("}"); 111 return s.toString(); 112 } 113 /** Converts the Moc to its basic ASCII representation as described in the MOC 114 standard document. The result is well-formed. */ mocToStringASCII(Moc moc)115 public static String mocToStringASCII (Moc moc) 116 { return mocToStringGeneral(moc,false); } 117 /** Converts the Moc to its JSON representation as described in the MOC 118 standard document. The result is well-formed. */ mocToStringJSON(Moc moc)119 public static String mocToStringJSON(Moc moc) 120 { return mocToStringGeneral(moc,true); } 121 } 122