/
Is24XmlDocument.java
145 lines (136 loc) · 4.17 KB
/
Is24XmlDocument.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
/*
* 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.is24_xml;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import org.openestate.io.core.XmlDocument;
import org.openestate.io.core.XmlUtils;
import org.openestate.io.is24_xml.xml.IS24ImmobilienTransfer;
import org.openestate.io.is24_xml.xml.ImmobilienTransferTyp;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
/**
* IS24-XML document with a <IS24ImmobilienTransfer> root element.
*
* @since 1.0
* @author Andreas Rudolph
*/
public class Is24XmlDocument extends XmlDocument<ImmobilienTransferTyp>
{
//private final static Logger LOGGER = LoggerFactory.getLogger( WisItDocument.class );
/**
* Create from a {@link Document}.
*
* @param document
* the document to create from
*/
public Is24XmlDocument( Document document )
{
super( document );
if (!isReadable( document ))
throw new IllegalArgumentException( "The provided document is invalid!" );
}
/**
* Checks, if a {@link Document} is readable as a {@link Is24XmlDocument}.
*
* @param doc
* document to check
*
* @return
* true, if the document is usable, otherwise false
*/
public static boolean isReadable( Document doc )
{
Element root = XmlUtils.getRootElement( doc );
return "IS24ImmobilienTransfer".equals( root.getLocalName() );
}
/**
* Creates an empty {@link Is24XmlDocument}.
*
* @return
* created document
*
* @throws ParserConfigurationException
* if the parser is not properly configured
*
* @throws JAXBException
* if a problem with JAXB occured
*/
public static Is24XmlDocument newDocument() throws ParserConfigurationException, JAXBException
{
return newDocument( Is24XmlUtils.getFactory().createImmobilienTransferTyp() );
}
/**
* Creates a {@link Is24XmlDocument} from a {@link ImmobilienTransferTyp}
* object.
*
* @param transfer
* Java object, that represents the <IS24ImmobilienTransfer> root
* element
*
* @return
* created document
*
* @throws ParserConfigurationException
* if the parser is not properly configured
*
* @throws JAXBException
* if a problem with JAXB occured
*/
public static Is24XmlDocument newDocument( ImmobilienTransferTyp transfer ) throws ParserConfigurationException, JAXBException
{
return newDocument( Is24XmlUtils.getFactory().createIS24ImmobilienTransfer( transfer ) );
}
/**
* Creates a {@link Is24XmlDocument} from a {@link ImmobilienTransferTyp}
* object.
*
* @param transfer
* Java object, that represents the <IS24ImmobilienTransfer> root
* element
*
* @return
* created document
*
* @throws ParserConfigurationException
* if the parser is not properly configured
*
* @throws JAXBException
* if a problem with JAXB occured
*/
public static Is24XmlDocument newDocument( IS24ImmobilienTransfer transfer ) throws ParserConfigurationException, JAXBException
{
Document document = XmlUtils.newDocument();
Is24XmlUtils.createMarshaller( "UTF-8", true ).marshal( transfer, document );
return new Is24XmlDocument( document );
}
/**
* Creates a {@link ImmobilienTransferTyp} object from the contained
* {@link Document}.
*
* @return
* created object, that represents the <IS24ImmobilienTransfer> root
* element
*
* @throws JAXBException
* if a problem with JAXB occured
*/
@Override
public ImmobilienTransferTyp toObject() throws JAXBException
{
return ((IS24ImmobilienTransfer) Is24XmlUtils.createUnmarshaller().unmarshal( this.getDocument() )).getValue();
}
}