Skip to content

Commit 556dbdb

Browse files
mkoncekmizdebsk
authored andcommitted
Replace JAXB parser
Fixes #23
1 parent be50418 commit 556dbdb

20 files changed

Lines changed: 871 additions & 171 deletions

xmvn-tools/xmvn-resolve/src/main/java/org/fedoraproject/xmvn/tools/resolve/ResolverCli.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
*/
1616
package org.fedoraproject.xmvn.tools.resolve;
1717

18+
import java.io.IOException;
1819
import java.util.ArrayList;
1920
import java.util.Collections;
2021
import java.util.List;
2122
import java.util.stream.Collectors;
2223

23-
import javax.xml.bind.JAXBContext;
24-
import javax.xml.bind.JAXBException;
25-
import javax.xml.bind.Marshaller;
26-
import javax.xml.bind.Unmarshaller;
24+
import javax.xml.stream.XMLStreamException;
2725

2826
import org.fedoraproject.xmvn.artifact.Artifact;
2927
import org.fedoraproject.xmvn.artifact.DefaultArtifact;
@@ -32,8 +30,8 @@
3230
import org.fedoraproject.xmvn.resolver.ResolutionRequest;
3331
import org.fedoraproject.xmvn.resolver.ResolutionResult;
3432
import org.fedoraproject.xmvn.resolver.Resolver;
35-
import org.fedoraproject.xmvn.tools.resolve.xml.CompoundRequest;
36-
import org.fedoraproject.xmvn.tools.resolve.xml.CompoundResult;
33+
import org.fedoraproject.xmvn.tools.resolve.xml.ResolutionRequestListUnmarshaller;
34+
import org.fedoraproject.xmvn.tools.resolve.xml.ResolutionResultListMarshaller;
3735

3836
/**
3937
* Resolve artifacts given on command line.
@@ -53,14 +51,11 @@ public ResolverCli( Resolver resolver )
5351
}
5452

5553
private List<ResolutionRequest> parseRequests( ResolverCliRequest cli )
56-
throws JAXBException
54+
throws IOException, XMLStreamException
5755
{
5856
if ( cli.isRaw() )
5957
{
60-
JAXBContext jaxbContext = JAXBContext.newInstance( CompoundRequest.class );
61-
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
62-
CompoundRequest compoundRequest = (CompoundRequest) jaxbUnmarshaller.unmarshal( System.in );
63-
List<ResolutionRequest> requests = compoundRequest.getRequests();
58+
List<ResolutionRequest> requests = new ResolutionRequestListUnmarshaller( System.in ).unmarshal();
6459
return requests != null ? requests : Collections.<ResolutionRequest>emptyList();
6560
}
6661

@@ -83,14 +78,11 @@ private List<ResolutionRequest> parseRequests( ResolverCliRequest cli )
8378
}
8479

8580
private void printResults( ResolverCliRequest cli, List<ResolutionResult> results )
86-
throws JAXBException
81+
throws IOException, XMLStreamException
8782
{
8883
if ( cli.isRaw() )
8984
{
90-
JAXBContext jaxbContext = JAXBContext.newInstance( CompoundResult.class );
91-
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
92-
jaxbMarshaller.setProperty( Marshaller.JAXB_FORMATTED_OUTPUT, true );
93-
jaxbMarshaller.marshal( new CompoundResult( results ), System.out );
85+
new ResolutionResultListMarshaller( results ).marshal( System.out );
9486
}
9587
else if ( cli.isClasspath() )
9688
{
@@ -103,7 +95,7 @@ else if ( cli.isClasspath() )
10395
}
10496

10597
private void run( ResolverCliRequest cliRequest )
106-
throws JAXBException
98+
throws IOException, XMLStreamException
10799
{
108100
try
109101
{

xmvn-tools/xmvn-resolve/src/main/java/org/fedoraproject/xmvn/tools/resolve/xml/ArtifactBean.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@
1515
*/
1616
package org.fedoraproject.xmvn.tools.resolve.xml;
1717

18-
import javax.xml.bind.annotation.XmlRootElement;
19-
import javax.xml.bind.annotation.adapters.XmlAdapter;
20-
2118
import org.fedoraproject.xmvn.artifact.Artifact;
2219
import org.fedoraproject.xmvn.artifact.DefaultArtifact;
2320

2421
/**
2522
* @author Mikolaj Izdebski
2623
*/
27-
@XmlRootElement( name = "artifact" )
2824
public class ArtifactBean
2925
{
3026
private String groupId;
@@ -91,14 +87,12 @@ public void setVersion( String version )
9187
* @author Mikolaj Izdebski
9288
*/
9389
public static class Adapter
94-
extends XmlAdapter<ArtifactBean, Artifact>
9590
{
9691
private static String nullify( String value, String defaultValue )
9792
{
9893
return value.equals( defaultValue ) ? null : value;
9994
}
10095

101-
@Override
10296
public ArtifactBean marshal( Artifact artifact )
10397
throws Exception
10498
{
@@ -113,7 +107,6 @@ public ArtifactBean marshal( Artifact artifact )
113107
return bean;
114108
}
115109

116-
@Override
117110
public Artifact unmarshal( ArtifactBean bean )
118111
throws Exception
119112
{
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*-
2+
* Copyright (c) 2018 Red Hat, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.fedoraproject.xmvn.tools.resolve.xml;
17+
18+
import java.io.IOException;
19+
import java.nio.file.Path;
20+
import java.nio.file.Paths;
21+
22+
import javax.xml.stream.XMLEventReader;
23+
import javax.xml.stream.XMLStreamConstants;
24+
import javax.xml.stream.XMLStreamException;
25+
import javax.xml.stream.events.StartElement;
26+
import javax.xml.stream.events.XMLEvent;
27+
28+
import org.fedoraproject.xmvn.artifact.Artifact;
29+
30+
/**
31+
* @author Marian Koncek
32+
*/
33+
class ArtifactUnmarshaller
34+
{
35+
private XMLEventReader eventReader;
36+
37+
static class StringConstants
38+
{
39+
private static final String GROUP_ID = "groupId";
40+
41+
private static final String ARTIFACT_ID = "artifactId";
42+
43+
private static final String EXTENSION = "extension";
44+
45+
private static final String CLASSIFIER = "classifier";
46+
47+
private static final String VERSION = "version";
48+
49+
private static final String PATH = "path";
50+
}
51+
52+
public ArtifactUnmarshaller( XMLEventReader eventReader )
53+
{
54+
this.eventReader = eventReader;
55+
}
56+
57+
/**
58+
* @return A String representation of the nested element or an empty string if end of section has been found
59+
* @throws IOException
60+
* @throws XMLStreamException
61+
*/
62+
String readUntilEnd( String end )
63+
throws XMLStreamException
64+
{
65+
StringBuffer stringBuffer = new StringBuffer();
66+
67+
while ( eventReader.hasNext() )
68+
{
69+
XMLEvent event = eventReader.nextEvent();
70+
71+
if ( event.getEventType() == XMLStreamConstants.CHARACTERS )
72+
{
73+
stringBuffer.append( event.asCharacters().getData() );
74+
}
75+
else if ( event.getEventType() == XMLStreamConstants.END_ELEMENT
76+
&& event.asEndElement().getName().getLocalPart().equals( end ) )
77+
{
78+
return stringBuffer.toString();
79+
}
80+
}
81+
82+
throw new XMLStreamException( "XML stream does not have a proper format" );
83+
}
84+
85+
Artifact unmarshal()
86+
throws XMLStreamException
87+
{
88+
ArtifactBean artifactBean = new ArtifactBean();
89+
Path path = null;
90+
91+
while ( eventReader.hasNext() )
92+
{
93+
XMLEvent event = eventReader.nextEvent();
94+
95+
switch ( event.getEventType() )
96+
{
97+
case XMLStreamConstants.START_ELEMENT:
98+
StartElement startElement = event.asStartElement();
99+
String startName = startElement.getName().getLocalPart();
100+
101+
switch ( startName )
102+
{
103+
case StringConstants.GROUP_ID:
104+
artifactBean.setGroupId( readUntilEnd( StringConstants.GROUP_ID ) );
105+
if ( artifactBean.getGroupId().isEmpty() )
106+
{
107+
throw new XMLStreamException( "Xml read error: groupId must not be empty" );
108+
}
109+
break;
110+
111+
case StringConstants.ARTIFACT_ID:
112+
artifactBean.setArtifactId( readUntilEnd( StringConstants.ARTIFACT_ID ) );
113+
if ( artifactBean.getArtifactId().isEmpty() )
114+
{
115+
throw new XMLStreamException( "Xml read error: artifactId must not be empty" );
116+
}
117+
break;
118+
119+
case StringConstants.EXTENSION:
120+
artifactBean.setExtension( readUntilEnd( StringConstants.EXTENSION ) );
121+
break;
122+
123+
case StringConstants.CLASSIFIER:
124+
artifactBean.setClassifier( readUntilEnd( StringConstants.CLASSIFIER ) );
125+
break;
126+
127+
case StringConstants.VERSION:
128+
artifactBean.setVersion( readUntilEnd( StringConstants.VERSION ) );
129+
break;
130+
131+
case StringConstants.PATH:
132+
path = Paths.get( readUntilEnd( StringConstants.PATH ) );
133+
break;
134+
135+
default:
136+
continue;
137+
}
138+
break;
139+
140+
case XMLStreamConstants.END_ELEMENT:
141+
if ( event.asEndElement().getName().getLocalPart().equals( "artifact" ) )
142+
{
143+
try
144+
{
145+
return new ArtifactBean.Adapter().unmarshal( artifactBean ).setPath( path );
146+
}
147+
catch ( Exception e )
148+
{
149+
throw new XMLStreamException( "XML stream does not have a proper format", e );
150+
}
151+
}
152+
break;
153+
154+
default:
155+
continue;
156+
}
157+
}
158+
159+
throw new XMLStreamException( "XML stream does not have a proper format" );
160+
}
161+
}

xmvn-tools/xmvn-resolve/src/main/java/org/fedoraproject/xmvn/tools/resolve/xml/CompoundRequest.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

xmvn-tools/xmvn-resolve/src/main/java/org/fedoraproject/xmvn/tools/resolve/xml/CompoundResult.java

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)