下面列出了javax.xml.bind.Unmarshaller#setAdapter ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Template method that can be overridden by concrete JAXB marshallers
* for custom initialization behavior. Gets called after creation of JAXB
* {@code Marshaller}, and after the respective properties have been set.
* <p>The default implementation sets the
* {@link #setUnmarshallerProperties defined properties}, the
* {@link #setValidationEventHandler validation event handler}, the
* {@link #setSchemas schemas}, {@link #setUnmarshallerListener listener},
* and {@link #setAdapters adapters}.
*/
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
if (this.unmarshallerProperties != null) {
for (String name : this.unmarshallerProperties.keySet()) {
unmarshaller.setProperty(name, this.unmarshallerProperties.get(name));
}
}
if (this.unmarshallerListener != null) {
unmarshaller.setListener(this.unmarshallerListener);
}
if (this.validationEventHandler != null) {
unmarshaller.setEventHandler(this.validationEventHandler);
}
if (this.adapters != null) {
for (XmlAdapter<?, ?> adapter : this.adapters) {
unmarshaller.setAdapter(adapter);
}
}
if (this.schema != null) {
unmarshaller.setSchema(this.schema);
}
}
/**
* Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
* Gets called after creation of JAXB {@code Marshaller}, and after the respective properties have been set.
* <p>The default implementation sets the {@link #setUnmarshallerProperties(Map) defined properties}, the {@link
* #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
* schemas}, {@link #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) listener}, and
* {@link #setAdapters(XmlAdapter[]) adapters}.
*/
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
if (this.unmarshallerProperties != null) {
for (String name : this.unmarshallerProperties.keySet()) {
unmarshaller.setProperty(name, this.unmarshallerProperties.get(name));
}
}
if (this.unmarshallerListener != null) {
unmarshaller.setListener(this.unmarshallerListener);
}
if (this.validationEventHandler != null) {
unmarshaller.setEventHandler(this.validationEventHandler);
}
if (this.adapters != null) {
for (XmlAdapter<?, ?> adapter : this.adapters) {
unmarshaller.setAdapter(adapter);
}
}
if (this.schema != null) {
unmarshaller.setSchema(this.schema);
}
}
/**
* Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
* Gets called after creation of JAXB {@code Marshaller}, and after the respective properties have been set.
* <p>The default implementation sets the {@link #setUnmarshallerProperties(Map) defined properties}, the {@link
* #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
* schemas}, {@link #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) listener}, and
* {@link #setAdapters(XmlAdapter[]) adapters}.
*/
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
if (this.unmarshallerProperties != null) {
for (String name : this.unmarshallerProperties.keySet()) {
unmarshaller.setProperty(name, this.unmarshallerProperties.get(name));
}
}
if (this.unmarshallerListener != null) {
unmarshaller.setListener(this.unmarshallerListener);
}
if (this.validationEventHandler != null) {
unmarshaller.setEventHandler(this.validationEventHandler);
}
if (this.adapters != null) {
for (XmlAdapter<?, ?> adapter : this.adapters) {
unmarshaller.setAdapter(adapter);
}
}
if (this.schema != null) {
unmarshaller.setSchema(this.schema);
}
}
void importRooms(File base) throws Exception {
log.info("Users import complete, starting room import");
Class<Room> eClazz = Room.class;
JAXBContext jc = JAXBContext.newInstance(eClazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new UserAdapter(userDao, userMap));
readList(unmarshaller, base, "rooms.xml", ROOM_LIST_NODE, ROOM_NODE, eClazz, r -> {
Long roomId = r.getId();
// We need to reset ids as openJPA reject to store them otherwise
r.setId(null);
if (r.getModerators() != null) {
for (Iterator<RoomModerator> i = r.getModerators().iterator(); i.hasNext();) {
RoomModerator rm = i.next();
if (rm.getUser().getId() == null) {
i.remove();
}
}
}
r = roomDao.update(r, null);
roomMap.put(roomId, r.getId());
});
}
void importRoomGroups(File base) throws Exception {
log.info("Room import complete, starting room groups import");
Class<RoomGroup> eClazz = RoomGroup.class;
JAXBContext jc = JAXBContext.newInstance(eClazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new RoomAdapter(roomDao, roomMap));
unmarshaller.setAdapter(new GroupAdapter(groupDao, groupMap));
readList(unmarshaller, base, "rooms_organisation.xml", ROOM_GRP_LIST_NODE, ROOM_GRP_NODE, eClazz, rg -> {
if (rg.getRoom() == null || rg.getGroup() == null) {
return;
}
Room r = roomDao.get(rg.getRoom().getId());
if (r == null || rg.getGroup().getId() == null) {
return;
}
if (r.getGroups() == null) {
r.setGroups(new ArrayList<>());
}
rg.setId(null);
rg.setRoom(r);
r.getGroups().add(rg);
roomDao.update(r, null);
});
}
void importChat(File base) throws Exception {
log.info("Room groups import complete, starting chat messages import");
Class<ChatMessage> eClazz = ChatMessage.class;
JAXBContext jc = JAXBContext.newInstance(eClazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new UserAdapter(userDao, userMap));
unmarshaller.setAdapter(new RoomAdapter(roomDao, roomMap));
readList(unmarshaller, base, "chat_messages.xml", CHAT_LIST_NODE, CHAT_NODE, eClazz, m -> {
m.setId(null);
if (m.getFromUser() == null || m.getFromUser().getId() == null
|| (m.getToRoom() != null && m.getToRoom().getId() == null)
|| (m.getToUser() != null && m.getToUser().getId() == null))
{
return;
}
chatDao.update(m, m.getSent());
});
}
private void importContacts(File base) throws Exception {
log.info("Private message folder import complete, starting user contacts import");
Class<UserContact> eClazz = UserContact.class;
JAXBContext jc = JAXBContext.newInstance(eClazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new UserAdapter(userDao, userMap));
readList(unmarshaller, base, "userContacts.xml", CONTACT_LIST_NODE, CONTACT_NODE, eClazz, uc -> {
Long ucId = uc.getId();
UserContact storedUC = userContactDao.get(ucId);
if (storedUC == null && uc.getContact() != null && uc.getContact().getId() != null) {
uc.setId(null);
if (uc.getOwner() != null && uc.getOwner().getId() == null) {
uc.setOwner(null);
}
uc = userContactDao.update(uc);
userContactMap.put(ucId, uc.getId());
}
});
}
private void importPolls(File base) throws Exception {
log.info("File explorer item import complete, starting room poll import");
Class<RoomPoll> eClazz = RoomPoll.class;
JAXBContext jc = JAXBContext.newInstance(eClazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new UserAdapter(userDao, userMap));
unmarshaller.setAdapter(new RoomAdapter(roomDao, roomMap));
readList(unmarshaller, base, "roompolls.xml", POLL_LIST_NODE, POLL_NODE, eClazz, rp -> {
rp.setId(null);
if (rp.getRoom() == null || rp.getRoom().getId() == null) {
//room was deleted
return;
}
if (rp.getCreator() == null || rp.getCreator().getId() == null) {
rp.setCreator(null);
}
for (RoomPollAnswer rpa : rp.getAnswers()) {
if (rpa.getVotedUser() == null || rpa.getVotedUser().getId() == null) {
rpa.setVotedUser(null);
}
}
pollDao.update(rp);
});
}
private void importRoomFiles(File base) throws Exception {
log.info("Poll import complete, starting room files import");
Class<RoomFile> eClazz = RoomFile.class;
JAXBContext jc = JAXBContext.newInstance(eClazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new FileAdapter(fileItemDao, fileItemMap));
readList(unmarshaller, base, "roomFiles.xml", ROOM_FILE_LIST_NODE, ROOM_FILE_NODE, eClazz, rf -> {
Room r = roomDao.get(roomMap.get(rf.getRoomId()));
if (r == null || rf.getFile() == null || rf.getFile().getId() == null) {
return;
}
if (r.getFiles() == null) {
r.setFiles(new ArrayList<>());
}
rf.setId(null);
rf.setRoomId(r.getId());
r.getFiles().add(rf);
roomDao.update(r, null);
}, true);
}
private void openFile(final File fileToOpen) {
try {
final JAXBContext jaxbContext = JAXBContext.newInstance(Configuration.class);
final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
unmarshaller.setAdapter(new FileXmlAdapter(fileToOpen.getParentFile()));
setConfiguration((Configuration) unmarshaller.unmarshal(fileToOpen));
MainFrame.this.file = fileToOpen;
addRecentFile(fileToOpen);
setChanged(false);
} catch (final JAXBException e1) {
e1.printStackTrace();
JOptionPane.showMessageDialog(MainFrame.this,
String.format(resourceBundle.getString("ui.mainframe.dialog.message.openconfig.error"), e1.getMessage()),
errorTitle, JOptionPane.ERROR_MESSAGE);
}
}
@Override
public Stream<Rule> stream() throws Exception {
final Unmarshaller unmarshaller = JAXBContext
.newInstance(RulesXml.RulesContext.class)
.createUnmarshaller();
unmarshaller.setEventHandler(event -> false);
unmarshaller.setAdapter(new RulesXml.XpathAdapter(this.xpath));
unmarshaller.setAdapter(new PatternAdapter(this.safeness));
InputStream inputStream = Files.newInputStream(this.source.path());
Object context = unmarshaller.unmarshal(inputStream);
return ((RulesXml.RulesContext) context).rules.stream();
}
void importCalendars(File base) throws Exception {
log.info("Chat messages import complete, starting calendar import");
Class<OmCalendar> eClazz = OmCalendar.class;
JAXBContext jc = JAXBContext.newInstance(eClazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new UserAdapter(userDao, userMap));
readList(unmarshaller, base, "calendars.xml", CALENDAR_LIST_NODE, CALENDAR_NODE, eClazz, c -> {
Long id = c.getId();
c.setId(null);
c = calendarDao.update(c);
calendarMap.put(id, c.getId());
}, true);
}
void importAppointments(File base) throws Exception {
log.info("Calendar import complete, starting appointement import");
Class<Appointment> eClazz = Appointment.class;
JAXBContext jc = JAXBContext.newInstance(eClazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new UserAdapter(userDao, userMap));
unmarshaller.setAdapter(new RoomAdapter(roomDao, roomMap));
unmarshaller.setAdapter(new OmCalendarAdapter(calendarDao, calendarMap));
readList(unmarshaller, base, "appointements.xml", APPOINTMENT_LIST_NODE, APPOINTMENT_NODE, eClazz, a -> {
Long appId = a.getId();
// We need to reset this as openJPA reject to store them otherwise
a.setId(null);
if (a.getOwner() != null && a.getOwner().getId() == null) {
a.setOwner(null);
}
if (a.getRoom() == null || a.getRoom().getId() == null) {
log.warn("Appointment without room was found, skipping: {}", a);
return;
}
if (a.getStart() == null || a.getEnd() == null) {
log.warn("Appointment without start/end time was found, skipping: {}", a);
return;
}
a = appointmentDao.update(a, null, false);
appointmentMap.put(appId, a.getId());
});
}
void importMeetingMembers(File base) throws Exception {
log.info("Appointement import complete, starting meeting members import");
Class<MeetingMember> eClazz = MeetingMember.class;
JAXBContext jc = JAXBContext.newInstance(eClazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new UserAdapter(userDao, userMap));
unmarshaller.setAdapter(new AppointmentAdapter(appointmentDao, appointmentMap));
readList(unmarshaller, base, "meetingmembers.xml", MMEMBER_LIST_NODE, MMEMBER_NODE, eClazz, ma -> {
ma.setId(null);
meetingMemberDao.update(ma);
});
}
/**
* @param type Class of object to be read in
* @param in input stream to read
* @param <T> class of object to be returned
* @return a T read from the input stream
* @throws ParserConfigurationException is the SAX parser can not be configured
* @throws SAXException if there is an xml problem
* @throws JAXBException if the xml cannot be marshalled into a T.
*/
public static <T> Object unmarshalHandlerChains(final Class<T> type, final InputStream in) throws ParserConfigurationException, SAXException, JAXBException {
final InputSource inputSource = new InputSource(in);
final SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
factory.setValidating(false);
final SAXParser parser = factory.newSAXParser();
final JAXBContext ctx = JaxbJavaee.getContext(type);
final Unmarshaller unmarshaller = ctx.createUnmarshaller();
unmarshaller.setEventHandler(new ValidationEventHandler() {
public boolean handleEvent(final ValidationEvent validationEvent) {
System.out.println(validationEvent);
return false;
}
});
final JaxbJavaee.HandlerChainsNamespaceFilter xmlFilter = new JaxbJavaee.HandlerChainsNamespaceFilter(parser.getXMLReader());
xmlFilter.setContentHandler(unmarshaller.getUnmarshallerHandler());
final HandlerChainsStringQNameAdapter adapter = new HandlerChainsStringQNameAdapter();
adapter.setHandlerChainsNamespaceFilter(xmlFilter);
unmarshaller.setAdapter(HandlerChainsStringQNameAdapter.class, adapter);
final SAXSource source = new SAXSource(xmlFilter, inputSource);
currentPublicId.set(new TreeSet<String>());
try {
return unmarshaller.unmarshal(source);
} finally {
currentPublicId.set(null);
}
}
@Override
protected void customizeUnmarshaller(Unmarshaller unmarshaller) {
unmarshaller.setAdapter(new MyCustomElementAdapter());
}
@Override
protected void customizeUnmarshaller(Unmarshaller unmarshaller) {
unmarshaller.setAdapter(new MyCustomElementAdapter());
}
@Override
protected void customizeUnmarshaller(Unmarshaller unmarshaller) {
unmarshaller.setAdapter(new MyCustomElementAdapter());
}
void importUsers(File base) throws Exception {
log.info("OAuth2 servers import complete, starting user import");
String jNameTimeZone = getDefaultTimezone();
//add existent emails from database
List<User> users = userDao.getAllUsers();
final Set<String> userEmails = new HashSet<>();
final Set<UserKey> userLogins = new HashSet<>();
for (User u : users){
if (u.getAddress() != null && !Strings.isEmpty(u.getAddress().getEmail())) {
userEmails.add(u.getAddress().getEmail());
}
userLogins.add(new UserKey(u));
}
Class<User> eClazz = User.class;
JAXBContext jc = JAXBContext.newInstance(eClazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
unmarshaller.setAdapter(new GroupAdapter(groupDao, groupMap));
int minLoginLength = getMinLoginLength();
readList(unmarshaller, base, "users.xml", USER_LIST_NODE, USER_NODE, eClazz, u -> {
if (u.getLogin() == null || u.isDeleted()) {
return;
}
// check that email is unique
if (u.getAddress() != null && u.getAddress().getEmail() != null && User.Type.USER == u.getType()) {
if (userEmails.contains(u.getAddress().getEmail())) {
log.warn("Email is duplicated for user {}", u);
String updateEmail = String.format("modified_by_import_<%s>%s", randomUUID(), u.getAddress().getEmail());
u.getAddress().setEmail(updateEmail);
}
userEmails.add(u.getAddress().getEmail());
}
if (u.getType() == User.Type.LDAP) {
if (u.getDomainId() != null && ldapMap.containsKey(u.getDomainId())) {
u.setDomainId(ldapMap.get(u.getDomainId()));
} else {
log.error("Unable to find Domain for ID: {}", u.getDomainId());
}
}
if (u.getType() == User.Type.OAUTH) {
if (u.getDomainId() != null && oauthMap.containsKey(u.getDomainId())) {
u.setDomainId(oauthMap.get(u.getDomainId()));
} else {
log.error("Unable to find Domain for ID: {}", u.getDomainId());
}
}
if (userLogins.contains(new UserKey(u))) {
log.warn("LOGIN is duplicated for USER {}", u);
String updateLogin = String.format("modified_by_import_<%s>%s", randomUUID(), u.getLogin());
u.setLogin(updateLogin);
}
userLogins.add(new UserKey(u));
if (u.getGroupUsers() != null) {
for (Iterator<GroupUser> iter = u.getGroupUsers().iterator(); iter.hasNext();) {
GroupUser gu = iter.next();
if (gu.getGroup().getId() == null) {
iter.remove();
continue;
}
gu.setUser(u);
}
}
if (u.getType() == User.Type.CONTACT && u.getLogin().length() < minLoginLength) {
u.setLogin(randomUUID().toString());
}
String tz = u.getTimeZoneId();
if (tz == null) {
u.setTimeZoneId(jNameTimeZone);
}
Long userId = u.getId();
u.setId(null);
if (u.getSipUser() != null && u.getSipUser().getId() != 0) {
u.getSipUser().setId(0);
}
if (AuthLevelUtil.hasLoginLevel(u.getRights()) && !Strings.isEmpty(u.getActivatehash())) {
u.setActivatehash(null);
}
if (u.getExternalType() != null) {
Group g = groupDao.getExternal(u.getExternalType());
u.addGroup(g);
}
userDao.update(u, Long.valueOf(-1));
userMap.put(userId, u.getId());
});
}
/**
* Replaces the {@code sis-metadata} adapter by this adapter.
*/
@Override
public void register(final Unmarshaller unmarshaller) {
unmarshaller.setAdapter(org.apache.sis.internal.jaxb.gml.SC_VerticalCRS.class, this);
}