/
ImmobiliareItUtils.java
634 lines (560 loc) · 17.1 KB
/
ImmobiliareItUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/*
* Copyright 2015 OpenEstate.org.
*
* Licensed 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 org.openestate.io.immobiliare_it;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.Currency;
import java.util.Locale;
import javax.xml.bind.DatatypeConverter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.commons.lang3.StringUtils;
import org.openestate.io.core.LocaleUtils;
import org.openestate.io.core.XmlUtils;
import org.openestate.io.core.XmlValidationHandler;
import org.openestate.io.immobiliare_it.xml.ObjectFactory;
import org.openestate.io.immobiliare_it.xml.types.Category;
import org.openestate.io.immobiliare_it.xml.types.EnergyUnit;
import org.openestate.io.immobiliare_it.xml.types.LandSizeUnit;
import org.openestate.io.immobiliare_it.xml.types.SizeUnit;
import org.openestate.io.immobiliare_it.xml.types.Transaction;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
* Some helper functions for the XML format of
* <a href="http://immobiliare.it/">immobiliare.it</a>.
*
* @since 1.0
* @author Andreas Rudolph
*/
public class ImmobiliareItUtils
{
//private final static Logger LOGGER = LoggerFactory.getLogger( ImmobiliareItUtils.class );
private static JAXBContext JAXB = null;
/**
* the latest implemented version of this format
*/
public final static ImmobiliareItVersion VERSION = ImmobiliareItVersion.V2_5;
/**
* the XML target namespace of this format
*/
public final static String NAMESPACE = "http://feed.immobiliare.it";
/**
* the default locale of this format
*/
public final static Locale DEFAULT_LOCALE = Locale.ENGLISH;
/**
* the package, where generated JAXB classes are located
*/
public final static String PACKAGE = "org.openestate.io.immobiliare_it.xml";
/**
* the factory for creation of JAXB objects
*/
public final static ObjectFactory FACTORY = new ObjectFactory();
private ImmobiliareItUtils()
{
}
/**
* Creates a {@link ImmobiliareItDocument} from an {@link InputStream}.
*
* @param input
* XML input
*
* @return
* created document or null, of the document is not supported by this format
*
* @throws SAXException
* if XML is invalid
*
* @throws IOException
* if reading failed
*
* @throws ParserConfigurationException
* if the parser is not properly configured
*/
public static ImmobiliareItDocument createDocument( InputStream input ) throws SAXException, IOException, ParserConfigurationException
{
return createDocument( XmlUtils.newDocument( input, true ) );
}
/**
* Creates a {@link ImmobiliareItDocument} from a {@link File}.
*
* @param xmlFile
* XML file
*
* @return
* created document or null, of the document is not supported by this format
*
* @throws SAXException
* if XML is invalid
*
* @throws IOException
* if reading failed
*
* @throws ParserConfigurationException
* if the parser is not properly configured
*/
public static ImmobiliareItDocument createDocument( File xmlFile ) throws SAXException, IOException, ParserConfigurationException
{
return createDocument( XmlUtils.newDocument( xmlFile, true ) );
}
/**
* Creates a {@link ImmobiliareItDocument} from a {@link String}.
*
* @param xmlString
* XML string
*
* @return
* created document or null, of the document is not supported by this format
*
* @throws SAXException
* if XML is invalid
*
* @throws IOException
* if reading failed
*
* @throws ParserConfigurationException
* if the parser is not properly configured
*/
public static ImmobiliareItDocument createDocument( String xmlString ) throws SAXException, IOException, ParserConfigurationException
{
return createDocument( XmlUtils.newDocument( xmlString, true ) );
}
/**
* Creates a {@link ImmobiliareItDocument} from a {@link Document}.
*
* @param doc
* XML document
*
* @return
* created document or null, of the document is not supported by this format
*/
public static ImmobiliareItDocument createDocument( Document doc )
{
if (ImmobiliareItDocument.isReadable( doc ))
return new ImmobiliareItDocument( doc );
else
return null;
}
/**
* Creates a {@link Marshaller} to write JAXB objects into XML.
*
* @return
* created marshaller
*
* @throws JAXBException
* if a problem with JAXB occured
*/
public static Marshaller createMarshaller() throws JAXBException
{
return createMarshaller( Charset.defaultCharset().name(), true );
}
/**
* Creates a {@link Marshaller} to write JAXB objects into XML.
*
* @param encoding
* encoding of written XML
*
* @param formatted
* if written XML is pretty printed
*
* @return
* created marshaller
*
* @throws JAXBException
* if a problem with JAXB occured
*/
public static Marshaller createMarshaller( String encoding, boolean formatted ) throws JAXBException
{
Marshaller m = getContext().createMarshaller();
m.setProperty( Marshaller.JAXB_ENCODING, encoding );
m.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, formatted );
m.setEventHandler( new XmlValidationHandler() );
return m;
}
/**
* Creates a {@link Unmarshaller} to read JAXB objects from XML.
*
* @return
* created unmarshaller
*
* @throws JAXBException
* if a problem with JAXB occured
*/
public static Unmarshaller createUnmarshaller() throws JAXBException
{
Unmarshaller m = getContext().createUnmarshaller();
m.setEventHandler( new XmlValidationHandler() );
return m;
}
/**
* Returns the {@link JAXBContext} for this format.
*
* @return
* context
*
* @throws JAXBException
* if a problem with JAXB occured
*/
public synchronized static JAXBContext getContext() throws JAXBException
{
if (JAXB==null) initContext( Thread.currentThread().getContextClassLoader() );
return JAXB;
}
/**
* Returns a country code, as it is preferred by this format.
*
* @param country
* country name to convert
*
* @return
* converted country code or null, if no matching country was found
*/
public static String getCountryCode( String country )
{
return LocaleUtils.getCountryISO2( country );
}
/**
* Returns the {@link ObjectFactory} for this format.
*
* @return
* object factory
*/
public synchronized static ObjectFactory getFactory()
{
return FACTORY;
}
/**
* Intializes the {@link JAXBContext} for this format.
*
* @param classloader
* the classloader to load the generated JAXB classes with
*
* @throws JAXBException
* if a problem with JAXB occured
*/
public synchronized static void initContext( ClassLoader classloader ) throws JAXBException
{
JAXB = JAXBContext.newInstance( PACKAGE, classloader );
}
public static boolean isValidDateUpdatedType( Calendar value )
{
final Calendar min = DatatypeConverter.parseDateTime( "2000-12-31T00:00:00" );
return value!=null && !value.before( min );
}
public static boolean isValidEmailType( String value )
{
return value!=null && value.matches( "[^@]+@[^\\.]+\\..+" );
}
public static boolean isValidLatitude( Double value )
{
return value!=null && value>=27.2 && value<=71.1;
}
public static boolean isValidLongitude( Double value )
{
return value!=null && value>=-31.2 && value<=38.9;
}
public static boolean isValidRooms( Integer value )
{
return value!=null && value>=1 && value<=100;
}
public static boolean isValidYear( Integer value )
{
return value!=null && value>=1000 && value<=2020;
}
public static Category parseCategory( String value )
{
value = StringUtils.trimToNull( value );
if (value==null) return null;
Category cat = Category.fromXmlValue( value );
if (cat==null)
throw new IllegalArgumentException( "Can't parse category value '" + value + "'!" );
return cat;
}
public static String parseCountry( String value )
{
return StringUtils.trimToNull( value );
}
public static Currency parseCurrency(String value)
{
value = StringUtils.trimToNull( value );
if (value==null) return null;
try
{
if (value.trim().length()!=3)
throw new Exception( "Currency code must contain of 3 characters." );
return Currency.getInstance( value.trim().toUpperCase() );
}
catch (Exception ex)
{
throw new IllegalArgumentException( "Can't parse currency value '" + value + "'!", ex );
}
}
public static Calendar parseDateUpdatedType( String value )
{
try
{
return XmlUtils.parseDateTime( value );
}
catch (Exception ex)
{
throw new IllegalArgumentException( "Can't parse date-updated value '"+value+"'!", ex );
}
}
public static String parseEmailType(String value)
{
//value = StringUtils.trimToNull( value );
//if (value==null)
// return null;
//else if (value.matches( "[^@]+@[^\\.]+\\..+" ))
// return value;
//else
// throw new IllegalArgumentException( "Can't parse e-mail value '" + value + "'!" );
return StringUtils.trimToNull( value );
}
public static EnergyUnit parseEnergyUnit( String value )
{
value = StringUtils.trimToNull( value );
if (value==null) return null;
EnergyUnit unit = EnergyUnit.fromXmlValue( value );
if (unit==null)
throw new IllegalArgumentException( "Can't parse energy-unit value '" + value + "'!" );
return unit;
}
public static Long parseInteger( String value )
{
value = StringUtils.trimToNull( value );
return (value!=null)? DatatypeConverter.parseLong( value ): null;
}
public static LandSizeUnit parseLandSizeUnit( String value )
{
value = StringUtils.trimToNull( value );
if (value==null) return null;
LandSizeUnit unit = LandSizeUnit.fromXmlValue( value );
if (unit==null)
throw new IllegalArgumentException( "Can't parse land-size-unit value '" + value + "'!" );
return unit;
}
public static Double parseLatitude( String value )
{
value = StringUtils.trimToNull( value );
return (value!=null)? DatatypeConverter.parseDouble( value ): null;
}
public static Double parseLongitude( String value )
{
value = StringUtils.trimToNull( value );
return (value!=null)? DatatypeConverter.parseDouble( value ): null;
}
public static Integer parseRooms( String value )
{
value = StringUtils.trimToNull( value );
return (value!=null)? DatatypeConverter.parseInt( value ): null;
}
public static SizeUnit parseSizeUnit( String value )
{
value = StringUtils.trimToNull( value );
if (value==null) return null;
SizeUnit unit = SizeUnit.fromXmlValue( value );
if (unit==null)
throw new IllegalArgumentException( "Can't parse size-unit value '" + value + "'!" );
return unit;
}
private static String parseText( String value, int length )
{
return StringUtils.trimToNull( value );
}
public static String parseText3000( String value )
{
return parseText( value, 3000 );
}
public static Transaction parseTransaction( String value )
{
value = StringUtils.trimToNull( value );
if (value==null) return null;
Transaction unit = Transaction.fromXmlValue( value );
if (unit==null)
throw new IllegalArgumentException( "Can't parse transaction value '" + value + "'!" );
return unit;
}
public static Integer parseYear( String value )
{
value = StringUtils.trimToNull( value );
return (value!=null)? DatatypeConverter.parseInt( value ): null;
}
public static Boolean parseYesNo( String value )
{
value = StringUtils.trimToEmpty( value );
if ("yes".equalsIgnoreCase( value ))
return Boolean.TRUE;
else if ("no".equalsIgnoreCase( value ))
return Boolean.FALSE;
else
throw new IllegalArgumentException( "Can't parse yes-no value '" + value + "'!" );
}
public static Boolean parseYN( String value )
{
value = StringUtils.trimToEmpty( value );
if ("y".equalsIgnoreCase( value ))
return Boolean.TRUE;
else if ("n".equalsIgnoreCase( value ))
return Boolean.FALSE;
else
throw new IllegalArgumentException( "Can't parse y-n value '" + value + "'!" );
}
public static String printCategory( Category value )
{
if (value==null)
throw new IllegalArgumentException( "Can't print category value!" );
else
return value.getXmlValue();
}
public static String printCountry( String value )
{
value = StringUtils.trimToNull( value );
if (value==null)
throw new IllegalArgumentException( "Can't print country value!" );
String iso2 = StringUtils.trimToNull( ImmobiliareItUtils.getCountryCode( value ) );
if (iso2==null)
throw new IllegalArgumentException( "Can't convert country '" + value + "' to its ISO2 code!" );
else
return iso2;
}
public static String printCurrency( Currency value)
{
if (value==null)
throw new IllegalArgumentException( "Can't print currency value!" );
else
return value.getCurrencyCode();
}
public static String printDateUpdatedType( Calendar value )
{
if (value==null || !isValidDateUpdatedType( value ))
throw new IllegalArgumentException( "Can't print date-updated value!" );
else
return DatatypeConverter.printDateTime( value );
}
public static String printEmailType( String value)
{
if (value==null || !isValidEmailType( value ))
throw new IllegalArgumentException( "Can't print email value!" );
else
return value;
}
public static String printEnergyUnit( EnergyUnit value )
{
if (value==null)
throw new IllegalArgumentException( "Can't print energy-unit value!" );
else
return value.getXmlValue();
}
public static String printInteger( Long value )
{
if (value==null)
throw new IllegalArgumentException( "Can't print integer value!" );
else
return DatatypeConverter.printLong( value );
}
public static String printLandSizeUnit( LandSizeUnit value )
{
if (value==null)
throw new IllegalArgumentException( "Can't print land-size-unit value!" );
else
return value.getXmlValue();
}
public static String printLatitude( Double value )
{
if (value==null || !isValidLatitude(value))
throw new IllegalArgumentException( "Can't print latitude value!" );
else
return DatatypeConverter.printDouble( value );
}
public static String printLongitude( Double value )
{
if (value==null || !isValidLongitude(value))
throw new IllegalArgumentException( "Can't print longitude value!" );
else
return DatatypeConverter.printDouble( value );
}
public static String printRooms( Integer value )
{
if (value==null || !isValidRooms(value))
throw new IllegalArgumentException( "Can't print rooms value!" );
else
return DatatypeConverter.printInt( value );
}
public static String printSizeUnit( SizeUnit value )
{
if (value==null)
throw new IllegalArgumentException( "Can't print size-unit value!" );
else
return value.getXmlValue();
}
private static String printText( String value, int maxLength )
{
value = StringUtils.trimToEmpty( value );
int length = value.length();
if (length<=0)
return StringUtils.EMPTY;
else if (length<=maxLength)
return value;
else if (maxLength>3)
return StringUtils.abbreviate( value, maxLength );
else
return value.substring( 0, maxLength );
}
public static String printText3000( String value )
{
return printText( value, 3000 );
}
public static String printTransaction( Transaction value )
{
if (value==null)
throw new IllegalArgumentException( "Can't print transaction value!" );
else
return value.getXmlValue();
}
public static String printYear( Integer value )
{
if (value==null || !isValidYear(value))
throw new IllegalArgumentException( "Can't print year value!" );
else
return DatatypeConverter.printInt( value );
}
public static String printYesNo( Boolean value )
{
if (Boolean.TRUE.equals( value ))
return "yes";
else if (Boolean.FALSE.equals( value ))
return "no";
else
throw new IllegalArgumentException( "Can't print yes-no value!" );
}
public static String printYN( Boolean value )
{
if (Boolean.TRUE.equals( value ))
return "Y";
else if (Boolean.FALSE.equals( value ))
return "N";
else
throw new IllegalArgumentException( "Can't print y-n value!" );
}
}