Skip to content

Commit c36b41e

Browse files
author
Tanechka
committed
Lesson10 xml
1 parent cf53271 commit c36b41e

12 files changed

Lines changed: 168 additions & 25 deletions

File tree

src/ru/javawebinar/basejava/model/Link.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
11
package ru.javawebinar.basejava.model;
22

3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
35
import java.io.Serializable;
46
import java.util.Objects;
57

68
/**
79
* gkislin
810
* 14.07.2016
911
*/
12+
@XmlAccessorType(XmlAccessType.FIELD)
1013
public class Link implements Serializable {
1114
private static final long serialVersionUID = 1L;
1215

13-
private final String name;
14-
private final String url;
16+
private String name;
17+
private String url;
18+
19+
public Link() {
20+
}
1521

1622
public Link(String name, String url) {
1723
Objects.requireNonNull(name, "name must not be null");

src/ru/javawebinar/basejava/model/ListSection.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ public class ListSection extends Section {
1212

1313
private static final long serialVersionUID = 1L;
1414

15-
private final List<String> items;
15+
private List<String> items;
16+
17+
public ListSection() {
18+
}
1619

1720
public ListSection(String... items) {
1821
this(Arrays.asList(items));

src/ru/javawebinar/basejava/model/Organization.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package ru.javawebinar.basejava.model;
22

3+
import ru.javawebinar.basejava.util.LocalDateAdapter;
4+
5+
import javax.xml.bind.annotation.XmlAccessType;
6+
import javax.xml.bind.annotation.XmlAccessorType;
7+
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
38
import java.io.Serializable;
49
import java.time.LocalDate;
510
import java.time.Month;
@@ -11,12 +16,16 @@
1116
import static ru.javawebinar.basejava.util.DateUtil.NOW;
1217
import static ru.javawebinar.basejava.util.DateUtil.of;
1318

19+
@XmlAccessorType(XmlAccessType.FIELD)
1420
public class Organization implements Serializable {
1521
private static final long serialVersionUID = 1L;
1622

17-
private final Link homePage;
23+
private Link homePage;
1824
private List<Position> positions = new ArrayList<>();
1925

26+
public Organization() {
27+
}
28+
2029
public Organization(String name, String url, Position... positions) {
2130
this(new Link(name, url), Arrays.asList(positions));
2231
}
@@ -49,11 +58,17 @@ public String toString() {
4958
* gkislin
5059
* 28.07.2016
5160
*/
52-
public static class Position implements Serializable{
53-
private final LocalDate startDate;
54-
private final LocalDate endDate;
55-
private final String title;
56-
private final String description;
61+
@XmlAccessorType(XmlAccessType.FIELD)
62+
public static class Position implements Serializable {
63+
@XmlJavaTypeAdapter(LocalDateAdapter.class)
64+
private LocalDate startDate;
65+
@XmlJavaTypeAdapter(LocalDateAdapter.class)
66+
private LocalDate endDate;
67+
private String title;
68+
private String description;
69+
70+
public Position() {
71+
}
5772

5873
public Position(int startYear, Month startMonth, String title, String description) {
5974
this(of(startYear, startMonth), NOW, title, description);

src/ru/javawebinar/basejava/model/OrganizationSection.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package ru.javawebinar.basejava.model;
22

3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
35
import java.util.Arrays;
46
import java.util.List;
57
import java.util.Objects;
@@ -8,11 +10,13 @@
810
* gkislin
911
* 19.07.2016
1012
*/
13+
@XmlAccessorType(XmlAccessType.FIELD)
1114
public class OrganizationSection extends Section {
12-
1315
private static final long serialVersionUID = 1L;
1416

15-
private final List<Organization> organizations;
17+
private List<Organization> organizations;
18+
19+
public OrganizationSection() {}
1620

1721
public OrganizationSection(Organization... organizations) {
1822
this(Arrays.asList(organizations));

src/ru/javawebinar/basejava/model/Resume.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
11
package ru.javawebinar.basejava.model;
22

3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
5+
import javax.xml.bind.annotation.XmlRootElement;
36
import java.io.Serializable;
47
import java.util.EnumMap;
58
import java.util.Map;
69
import java.util.Objects;
710
import java.util.UUID;
811

12+
@XmlRootElement
13+
@XmlAccessorType(XmlAccessType.FIELD)
914
public class Resume implements Comparable<Resume>, Serializable {
1015
private static final long serialVersionUID = 1L;
16+
1117
// Unique identifier
12-
private final String uuid;
18+
private String uuid;
1319

14-
private final String fullName;
20+
private String fullName;
1521

1622
private final Map<ContactType, String> contacts = new EnumMap<>(ContactType.class);
1723
private final Map<SectionType, Section> sections = new EnumMap<>(SectionType.class);
1824

25+
public Resume() {
26+
}
27+
1928
public Resume(String fullName) {
2029
this(UUID.randomUUID().toString(), fullName);
2130
}
@@ -51,19 +60,16 @@ public void addSection(SectionType type, Section section) {
5160
public boolean equals(Object o) {
5261
if (this == o) return true;
5362
if (o == null || getClass() != o.getClass()) return false;
54-
5563
Resume resume = (Resume) o;
56-
57-
if (!uuid.equals(resume.uuid)) return false;
58-
return fullName.equals(resume.fullName);
59-
64+
return Objects.equals(uuid, resume.uuid) &&
65+
Objects.equals(fullName, resume.fullName) &&
66+
Objects.equals(contacts, resume.contacts) &&
67+
Objects.equals(sections, resume.sections);
6068
}
6169

6270
@Override
6371
public int hashCode() {
64-
int result = uuid.hashCode();
65-
result = 31 * result + fullName.hashCode();
66-
return result;
72+
return Objects.hash(uuid, fullName, contacts, sections);
6773
}
6874

6975
@Override
@@ -76,4 +82,4 @@ public int compareTo(Resume o) {
7682
int cmp = fullName.compareTo(o.fullName);
7783
return cmp != 0 ? cmp : uuid.compareTo(o.uuid);
7884
}
79-
}
85+
}
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
package ru.javawebinar.basejava.model;
22

3+
import javax.xml.bind.annotation.XmlAccessType;
4+
import javax.xml.bind.annotation.XmlAccessorType;
35
import java.io.Serializable;
46

57
/**
68
* gkislin
79
* 19.07.2016
810
*/
9-
abstract public class Section implements Serializable{
11+
@XmlAccessorType(XmlAccessType.FIELD)
12+
abstract public class Section implements Serializable {
1013
}

src/ru/javawebinar/basejava/model/TextSection.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ public class TextSection extends Section {
66

77
private static final long serialVersionUID = 1L;
88

9-
private final String content;
9+
private String content;
10+
11+
public TextSection() {
12+
}
1013

1114
public TextSection(String content) {
1215
Objects.requireNonNull(content, "content must not be null");
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package ru.javawebinar.basejava.storage.serializer;
2+
3+
import ru.javawebinar.basejava.model.*;
4+
import ru.javawebinar.basejava.util.XmlParser;
5+
6+
import java.io.*;
7+
import java.nio.charset.StandardCharsets;
8+
9+
public class XmlStreamSerializer implements StreamSerializer {
10+
private XmlParser xmlParser;
11+
12+
public XmlStreamSerializer() {
13+
xmlParser = new XmlParser(
14+
Resume.class, Organization.class, Link.class,
15+
OrganizationSection.class, TextSection.class, ListSection.class, Organization.Position.class);
16+
}
17+
18+
@Override
19+
public void doWrite(Resume r, OutputStream os) throws IOException {
20+
try (Writer w = new OutputStreamWriter(os, StandardCharsets.UTF_8)) {
21+
xmlParser.marshall(r, w);
22+
}
23+
}
24+
25+
@Override
26+
public Resume doRead(InputStream is) throws IOException {
27+
try (Reader r = new InputStreamReader(is, StandardCharsets.UTF_8)) {
28+
return xmlParser.unmarshall(r);
29+
}
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package ru.javawebinar.basejava.util;
2+
3+
import javax.xml.bind.annotation.adapters.XmlAdapter;
4+
import java.time.LocalDate;
5+
6+
public class LocalDateAdapter extends XmlAdapter<String, LocalDate> {
7+
@Override
8+
public LocalDate unmarshal(String str) throws Exception {
9+
return LocalDate.parse(str);
10+
}
11+
12+
@Override
13+
public String marshal(LocalDate ld) throws Exception {
14+
return ld.toString();
15+
}
16+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package ru.javawebinar.basejava.util;
2+
3+
import javax.xml.bind.JAXBContext;
4+
import javax.xml.bind.JAXBException;
5+
import javax.xml.bind.Marshaller;
6+
import javax.xml.bind.Unmarshaller;
7+
import java.io.Reader;
8+
import java.io.Writer;
9+
10+
public class XmlParser {
11+
private final Marshaller marshaller;
12+
private final Unmarshaller unmarshaller;
13+
14+
public XmlParser(Class... classesToBeBound) {
15+
try {
16+
JAXBContext ctx = JAXBContext.newInstance(classesToBeBound);
17+
18+
marshaller = ctx.createMarshaller();
19+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
20+
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
21+
// marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
22+
23+
unmarshaller = ctx.createUnmarshaller();
24+
} catch (JAXBException e) {
25+
throw new IllegalStateException(e);
26+
}
27+
}
28+
29+
public <T> T unmarshall(Reader reader) {
30+
try {
31+
return (T) unmarshaller.unmarshal(reader);
32+
} catch (JAXBException e) {
33+
throw new IllegalStateException(e);
34+
}
35+
}
36+
37+
public void marshall(Object instance, Writer writer) {
38+
try {
39+
marshaller.marshal(instance, writer);
40+
} catch (JAXBException e) {
41+
throw new IllegalStateException(e);
42+
}
43+
}
44+
}
45+

0 commit comments

Comments
 (0)