11package com .github .nosql ;
22
3+ import java .io .File ;
4+ import java .nio .charset .StandardCharsets ;
5+ import java .nio .file .Files ;
6+ import java .nio .file .Paths ;
7+ import java .util .Arrays ;
8+ import java .util .List ;
9+ import java .util .Map ;
10+
311import com .github .underscore .lodash .Json ;
412import com .github .underscore .lodash .U ;
513import com .github .underscore .lodash .Xml ;
14+ import java .nio .file .Path ;
15+ import java .util .ArrayList ;
16+ import java .util .LinkedHashMap ;
617import org .apache .commons .cli .Options ;
18+ import org .apache .commons .cli .BasicParser ;
19+ import org .apache .commons .cli .CommandLine ;
20+ import org .apache .commons .cli .CommandLineParser ;
21+ import org .apache .commons .cli .HelpFormatter ;
22+ import org .apache .commons .cli .ParseException ;
723
824public class Nosql {
25+ private static final String HEADER = "Nosql translation tool" ;
26+ private static final String MESSAGE = "For docs, license, tests, and downloads, see: https://github.com/php4dev/nosql-translations" ;
27+
928 /**
1029 * @param args the command line arguments
1130 */
@@ -14,6 +33,98 @@ public static void main(String args[]) throws Exception {
1433 options .addOption ("s" , "src" , true , "The source file/directory which should be converted from xml/json." );
1534 options .addOption ("d" , "dest" , true , "The destanation file/directory for converted file(s)." );
1635 options .addOption ("mo" , "mode" , true , "The processing file mode (xmltojson|jsontoxml)." );
36+ options .addOption ("?" , "help" , false , "This help text." );
37+ if (args .length > 0 ) {
38+ CommandLineParser parser = new BasicParser ();
39+ CommandLine cmd = null ;
40+ try {
41+ cmd = parser .parse (options , args );
42+ } catch (ParseException e ) {
43+ help (options );
44+ return ;
45+ }
46+
47+ if (cmd .hasOption ("?" ) || (!cmd .hasOption ("s" ) && !cmd .hasOption ("c" ))) {
48+ help (options );
49+ return ;
50+ }
51+
52+ processCommandLine (cmd );
53+ return ;
54+ }
55+ help (options );
56+ }
57+
58+ private static void help (Options options ) {
59+ HelpFormatter formater = new HelpFormatter ();
60+ formater .printHelp (100 , "java -jar nosql.jar" , HEADER , options , MESSAGE );
61+ }
62+
63+ private static void processCommandLine (CommandLine cmd ) throws Exception {
64+ String sourceFileOrPath = "." ;
65+ String targetFileOrPath = "." ;
66+ String mode = "xmltojson" ;
67+ if (cmd .hasOption ("s" )) {
68+ sourceFileOrPath = cmd .getOptionValue ("s" );
69+ }
70+ if (cmd .hasOption ("d" )) {
71+ targetFileOrPath = cmd .getOptionValue ("d" );
72+ }
73+ if (cmd .hasOption ("mo" )) {
74+ mode = cmd .getOptionValue ("mo" );
75+ }
76+ if ("xmltojson" .equals (mode )) {
77+ if (!new File (sourceFileOrPath ).isFile ()) {
78+ System .out .println ("File not found: " + sourceFileOrPath );
79+ }
80+ File file = new File (sourceFileOrPath );
81+ try {
82+ final byte [] bytes = Files .readAllBytes (Paths .get (file .getAbsolutePath (), new String [0 ]));
83+ String text = new String (removeBom (bytes ), StandardCharsets .UTF_8 );
84+ Map <String , Object > result = (Map <String , Object >) Xml .fromXml (text );
85+ List <Map <String , Object >> texts = (List <Map <String , Object >>) U .get (result , "document.texts.text" );
86+ System .out .println ("Found texts: " + texts .size ());
87+ List <Map <String , Object >> textsEn = new ArrayList <>();
88+ List <Map <String , Object >> textsDe = new ArrayList <>();
89+ List <Map <String , Object >> textsRu = new ArrayList <>();
90+ for (Map <String , Object > textItem : texts ) {
91+ Map <String , Object > textItemEn = new LinkedHashMap <>();
92+ textItemEn .put ("-id" , textItem .get ("-id" ));
93+ textItemEn .put ("en" , textItem .get ("en" ));
94+ textsEn .add (textItemEn );
95+ Map <String , Object > textItemDe = new LinkedHashMap <>();
96+ textItemDe .put ("-id" , textItem .get ("-id" ));
97+ textItemDe .put ("de" , textItem .get ("de" ));
98+ textsDe .add (textItemDe );
99+ Map <String , Object > textItemRu = new LinkedHashMap <>();
100+ textItemRu .put ("-id" , textItem .get ("-id" ));
101+ textItemRu .put ("ru" , textItem .get ("ru" ));
102+ textsRu .add (textItemRu );
103+ }
104+ final Path pathEn = Paths .get ("" , new String [] { file .getPath ().replaceFirst ("\\ .xml$" , "-en.json" ) });
105+ final Path pathDe = Paths .get ("" , new String [] { file .getPath ().replaceFirst ("\\ .xml$" , "-de.json" ) });
106+ final Path pathRu = Paths .get ("" , new String [] { file .getPath ().replaceFirst ("\\ .xml$" , "-ru.json" ) });
107+ Files .write (pathEn , Json .toJson (textsEn ).getBytes (StandardCharsets .UTF_8 ));
108+ Files .write (pathDe , Json .toJson (textsDe ).getBytes (StandardCharsets .UTF_8 ));
109+ Files .write (pathRu , Json .toJson (textsRu ).getBytes (StandardCharsets .UTF_8 ));
110+ } catch (Exception ex ) {
111+ System .out .println (file .getPath () + " - " + ex .getMessage ());
112+ }
113+ }
114+ }
115+
116+ private static byte [] removeBom (byte [] bytes ) {
117+ if ((bytes .length >= 3 ) && (bytes [0 ] == -17 ) && (bytes [1 ] == -69 ) && (bytes [2 ] == -65 )) {
118+ return Arrays .copyOfRange (bytes , 3 , bytes .length );
119+ }
120+ if ((bytes .length >= 2 ) && (bytes [0 ] == -1 ) && (bytes [1 ] == -2 )) {
121+ return Arrays .copyOfRange (bytes , 2 , bytes .length );
122+ }
123+ if ((bytes .length >= 2 ) && (bytes [0 ] == -2 ) && (bytes [1 ] == -1 )) {
124+ return Arrays .copyOfRange (bytes , 2 , bytes .length );
125+ }
126+ return bytes ;
17127 }
18128}
19129
130+
0 commit comments