private static Serializer getReadSerializer() throws Exception { if (readSerializer == null) { Log.d("hv", "Begin Creating Serializer"); RegistryMatcher matcher = new RegistryMatcher(); matcher.bind(Date.class, new DateFormatTransformer()); Registry registry = new Registry(); registry.bind(String.class, SimpleXMLStringConverter.class); Strategy strategy = new RegistryStrategy(registry); Serializer s = new Persister(strategy, matcher); Log.d("hv", "Done Creating Serializer"); readSerializer = s; } return readSerializer; }
private List<Item> getFeedItems(String feedUrl) throws Exception { ArrayList<Item> items = new ArrayList<>(); Request request = new Request.Builder().url(feedUrl).get().build(); Response response = client.newCall(request).execute(); if (response.isSuccessful()) { String data = response.body().string(); Timber.e("Got response from server: " + data); DateFormat rssDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); RegistryMatcher registryMatcher = new RegistryMatcher(); registryMatcher.bind(Date.class, new DateTransformer(rssDateFormat)); Serializer serializer = new Persister(registryMatcher); final Rss rss = serializer.read(Rss.class,data); if (rss != null && rss.channel != null && rss.channel.items.size() > 0) { items.addAll(rss.channel.items); } } return items; }
public ByteArrayOutputStream toXml(Object objectToSerialize) { try { RegistryMatcher matcher = new RegistryMatcher(); matcher.bind(Boolean.class, BooleanSimpleXmlAdapter.class); matcher.bind(GregorianCalendar.class, CalendarSimpleXmlAdapter.class); matcher.bind(UUID.class, UUIDSimpleXmlAdapter.class); matcher.bind(byte[].class, ByteSimpleXmlAdapter.class); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); TreeStrategyWithoutArrayLength strategy = new TreeStrategyWithoutArrayLength(); Serializer serializer = new Persister(strategy, matcher); serializer.write(objectToSerialize, outputStream); return outputStream; } catch (Exception e) { throw new KeePassDatabaseUnwriteableException("Could not serialize object to xml", e); } }
/** * Retrieve a {@link Serializer} instance suitable for deserialising a * {@link ConfigurationReport}. * * @return a new {@link Serializer} instance */ private Serializer getSerializer() { DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); RegistryMatcher matcher = new RegistryMatcher(); matcher.bind(Timestamp.class, new DateFormatConverter(format)); Strategy strategy = new AnnotationStrategy(); Serializer serializer = new Persister(strategy, matcher); return serializer; }
private void importRooms(File f) throws Exception { log.info("Users import complete, starting room import"); Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); RegistryMatcher matcher = new RegistryMatcher(); Serializer ser = new Persister(strategy, matcher); matcher.bind(Long.class, LongTransform.class); matcher.bind(Integer.class, IntegerTransform.class); registry.bind(User.class, new UserConverter(userDao, userMap)); registry.bind(Room.Type.class, RoomTypeConverter.class); registry.bind(Date.class, DateConverter.class); List<Room> list = readList(ser, f, "rooms.xml", "rooms", Room.class); for (Room r : list) { 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()); } }
private void importRecordings(File f) throws Exception { log.info("Meeting members import complete, starting recordings server import"); Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); RegistryMatcher matcher = new RegistryMatcher(); Serializer ser = new Persister(strategy, matcher); matcher.bind(Long.class, LongTransform.class); matcher.bind(Integer.class, IntegerTransform.class); registry.bind(Date.class, DateConverter.class); registry.bind(Recording.Status.class, RecordingStatusConverter.class); List<Recording> list = readList(ser, f, "flvRecordings.xml", "flvrecordings", Recording.class); for (Recording r : list) { Long recId = r.getId(); r.setId(null); if (r.getRoomId() != null) { r.setRoomId(roomMap.get(r.getRoomId())); } if (r.getOwnerId() != null) { r.setOwnerId(userMap.get(r.getOwnerId())); } if (r.getMetaData() != null) { for (RecordingMetaData meta : r.getMetaData()) { meta.setId(null); meta.setRecording(r); } } if (!Strings.isEmpty(r.getHash()) && r.getHash().startsWith(RECORDING_FILE_NAME)) { String name = getFileName(r.getHash()); r.setHash(UUID.randomUUID().toString()); fileMap.put(String.format(FILE_NAME_FMT, name, EXTENSION_JPG), String.format(FILE_NAME_FMT, r.getHash(), EXTENSION_PNG)); fileMap.put(String.format("%s.%s.%s", name, EXTENSION_FLV, EXTENSION_MP4), String.format(FILE_NAME_FMT, r.getHash(), EXTENSION_MP4)); } if (Strings.isEmpty(r.getHash())) { r.setHash(UUID.randomUUID().toString()); } r = recordingDao.update(r); fileItemMap.put(recId, r.getId()); } }
private List<FileItem> importFiles(File f) throws Exception { log.info("Private message import complete, starting file explorer item import"); List<FileItem> result = new ArrayList<>(); Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); RegistryMatcher matcher = new RegistryMatcher(); Serializer ser = new Persister(strategy, matcher); matcher.bind(Long.class, LongTransform.class); matcher.bind(Integer.class, IntegerTransform.class); registry.bind(Date.class, DateConverter.class); List<FileItem> list = readList(ser, f, "fileExplorerItems.xml", "fileExplorerItems", FileItem.class); for (FileItem file : list) { Long fId = file.getId(); // We need to reset this as openJPA reject to store them otherwise file.setId(null); Long roomId = file.getRoomId(); file.setRoomId(roomMap.containsKey(roomId) ? roomMap.get(roomId) : null); if (file.getOwnerId() != null) { file.setOwnerId(userMap.get(file.getOwnerId())); } if (file.getParentId() != null && file.getParentId().longValue() <= 0L) { file.setParentId(null); } if (Strings.isEmpty(file.getHash())) { file.setHash(UUID.randomUUID().toString()); } file = fileItemDao.update(file); result.add(file); fileItemMap.put(fId, file.getId()); } return result; }
private void importPolls(File f) throws Exception { log.info("File explorer item import complete, starting room poll import"); Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); RegistryMatcher matcher = new RegistryMatcher(); Serializer serializer = new Persister(strategy, matcher); matcher.bind(Integer.class, IntegerTransform.class); registry.bind(User.class, new UserConverter(userDao, userMap)); registry.bind(Room.class, new RoomConverter(roomDao, roomMap)); registry.bind(RoomPoll.Type.class, PollTypeConverter.class); registry.bind(Date.class, DateConverter.class); List<RoomPoll> list = readList(serializer, f, "roompolls.xml", "roompolls", RoomPoll.class); for (RoomPoll rp : list) { rp.setId(null); if (rp.getRoom() == null || rp.getRoom().getId() == null) { //room was deleted continue; } 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 static void addTransformers(@Nonnull RegistryMatcher matcher, @Nonnull Serializer serializer, @Nonnull Iterable<? extends TransformFactory> factories) { for (TransformFactory factory : factories) { // LOG.info("Factory " + factory); for (Map.Entry<? extends Class<?>, ? extends Transform<?>> e : factory.newTransforms()) { // LOG.info("Register " + e.getKey() + " -> " + e.getValue()); try { matcher.bind(e.getKey(), e.getValue()); } catch (Exception ex) { LOG.error("Failed to bind " + e.getKey() + " to " + e.getValue(), ex); } } } }
@Nonnull private static RegistryMatcher newRegistryMatcher() { RegistryMatcher matcher = new RegistryMatcher() { @Override public Transform match(Class type) throws Exception { if (Type.class.isAssignableFrom(type)) return TypeTransform.getInstance(); return super.match(type); } }; return matcher; }
@Provides Persister getPersister() { RegistryMatcher matchers = new RegistryMatcher(); matchers.bind(org.joda.time.DateTime.class, JodaTimeDateTimeTransform.class); Strategy strategy = new AnnotationStrategy(); return new Persister(strategy, matchers); }
private Serializer createSerializer() { RegistryMatcher matcher = new RegistryMatcher(); matcher.bind(Boolean.class, BooleanSimpleXmlAdapter.class); matcher.bind(GregorianCalendar.class, CalendarSimpleXmlAdapter.class); matcher.bind(UUID.class, UUIDSimpleXmlAdapter.class); matcher.bind(byte[].class, ByteSimpleXmlAdapter.class); TreeStrategyWithoutArrayLength strategy = new TreeStrategyWithoutArrayLength(); Serializer serializer = new Persister(strategy, matcher); return serializer; }
/** * Test starting point * * @param args * @throws Exception */ public static void main(String args[]) throws Exception { System.out.println("Start: " + new Date()); String filePath = System.getProperty("user.home") + "/SimpleObjectTest/web.xml"; if(args.length > 0) { filePath = args[0]; } RegistryMatcher matcher = new RegistryMatcher(); matcher.bind(ErrorCodeType.class, ErrorCodeTypeTransform.class); matcher.bind(ExceptionTypeType.class, ExceptionTypeTypeTransform.class); matcher.bind(ServletClassType.class, ServletClassTypeTransform.class); matcher.bind(JspFileType.class, JspFileTypeTransform.class); matcher.bind(UrlPatternType.class, UrlPatternTypeTransform.class); matcher.bind(ServletNameType.class, ServletNameTypeTransform.class); Serializer serializer = new Persister(matcher, new Format(4, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")); WebApp webApp = new WebApp(getIcon(), getDisplayName(), getDescription(), getDistributable(), getContextParams(), getFilters(), getFilterMappings(), getListeners(), getServlets(), getServletMappings(), getSessionConfig(), getMimeMappings(), getWelcomeFileList(), getErrorPages(), getTaglibs(), getResourceEnvRefs(), getResourceRefs(), getSecurityConstraints(), getLoginConfig(), getSecurityRoles(), getEnvEntrys(), getEjbRefs(), getEjbLocalRefs()); serializer.write(webApp, new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8")); System.out.println(" End: " + new Date()); }
public XmlSerializer(Class<T> classe) { classType = classe; try { RegistryMatcher m = new RegistryMatcher(); m.bind(byte[].class, new ByteArrayTransformer()); marshaller = new Persister(m); } catch (Exception ex) { throw new RuntimeException("Cannot instantiate JaxbSerializer for class: " + classType.getName(), ex); } }
public void performImport(InputStream is) throws Exception { userMap.clear(); groupMap.clear(); calendarMap.clear(); appointmentMap.clear(); roomMap.clear(); messageFolderMap.clear(); userContactMap.clear(); fileMap.clear(); messageFolderMap.put(INBOX_FOLDER_ID, INBOX_FOLDER_ID); messageFolderMap.put(SENT_FOLDER_ID, SENT_FOLDER_ID); messageFolderMap.put(TRASH_FOLDER_ID, TRASH_FOLDER_ID); File f = unzip(is); Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); RegistryMatcher matcher = new RegistryMatcher(); Serializer simpleSerializer = new Persister(strategy, matcher); matcher.bind(Long.class, LongTransform.class); registry.bind(Date.class, DateConverter.class); BackupVersion ver = getVersion(simpleSerializer, f); importConfigs(f); importGroups(f, simpleSerializer); Long defaultLdapId = importLdap(f, simpleSerializer); importOauth(f, simpleSerializer); importUsers(f, defaultLdapId); importRooms(f); importRoomGroups(f); importChat(f); importCalendars(f); importAppointments(f); importMeetingMembers(f); importRecordings(f); importPrivateMsgFolders(f, simpleSerializer); importContacts(f); importPrivateMsgs(f); List<FileItem> files = importFiles(f); importPolls(f); importRoomFiles(f); log.info("Room files import complete, starting copy of files and folders"); /* * ##################### Import real files and folders */ importFolders(f); if (ver.compareTo(BackupVersion.get("4.0.0")) < 0) { for (BaseFileItem bfi : files) { if (bfi.isDeleted()) { continue; } if (BaseFileItem.Type.Presentation == bfi.getType()) { convertOldPresentation((FileItem)bfi); fileItemDao._update(bfi); } if (BaseFileItem.Type.WmlFile == bfi.getType()) { try { Whiteboard wb = WbConverter.convert((FileItem)bfi); wb.save(bfi.getFile().toPath()); } catch (Exception e) { log.error("Unexpected error while converting WB", e); } } } } log.info("File explorer item import complete, clearing temp files"); FileUtils.deleteDirectory(f); }
private void importConfigs(File f) throws Exception { Registry registry = new Registry(); Strategy strategy = new RegistryStrategy(registry); RegistryMatcher matcher = new RegistryMatcher(); Serializer serializer = new Persister(strategy, matcher); matcher.bind(Long.class, LongTransform.class); registry.bind(Date.class, DateConverter.class); registry.bind(User.class, new UserConverter(userDao, userMap)); List<Configuration> list = readList(serializer, f, "configs.xml", "configs", Configuration.class); for (Configuration c : list) { if (c.getKey() == null || c.isDeleted()) { continue; } String newKey = outdatedConfigKeys.get(c.getKey()); if (newKey != null) { c.setKey(newKey); } Configuration.Type type = configTypes.get(c.getKey()); if (type != null) { c.setType(type); if (Configuration.Type.bool == type) { c.setValue(String.valueOf("1".equals(c.getValue()) || "yes".equals(c.getValue()) || "true".equals(c.getValue()))); } } Configuration cfg = cfgDao.forceGet(c.getKey()); if (cfg != null && !cfg.isDeleted()) { log.warn("Non deleted configuration with same key is found! old value: {}, new value: {}", cfg.getValue(), c.getValue()); } c.setId(cfg == null ? null : cfg.getId()); if (c.getUser() != null && c.getUser().getId() == null) { c.setUser(null); } if (CONFIG_CRYPT.equals(c.getKey())) { try { Class<?> clazz = Class.forName(c.getValue()); clazz.getDeclaredConstructor().newInstance(); } catch (Exception e) { log.warn("Not existing Crypt class found {}, replacing with SCryptImplementation", c.getValue()); c.setValue(SCryptImplementation.class.getCanonicalName()); } } cfgDao.update(c, null); } }
public SimpleXmlReader() { RegistryMatcher m = new RegistryMatcher(); m.bind(Date.class, new DateFormatTransformer()); serializer = new Persister(m); }
/** * Run the merge job. * * @throws Exception All errors with be thrown to the caller. */ public void runJob() throws Exception { RegistryMatcher matcher = new RegistryMatcher(); matcher.bind(ServletClassType.class, ServletClassTypeTransform.class); matcher.bind(JspFileType.class, JspFileTypeTransform.class); matcher.bind(UrlPatternType.class, UrlPatternTypeTransform.class); matcher.bind(ServletNameType.class, ServletNameTypeTransform.class); Serializer serializer = new Persister(matcher, new Format(4, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>")); WebApp webAppSrc1 = serializer.read(WebApp.class, new InputStreamReader(new FileInputStream(srcfile1), "UTF-8")); System.out.println(webAppSrc1); WebApp webAppSrc2 = serializer.read(WebApp.class, new InputStreamReader(new FileInputStream(srcfile2), "UTF-8")); System.out.println(webAppSrc2); WebApp webAppMerge = new WebApp(); WebApp webAppConflict = new WebApp(); if(webAppSrc1.getDisplayName() == null) { webAppMerge.setDisplayName(webAppSrc2.getDisplayName()); } else if(webAppSrc2.getDisplayName() == null) { webAppMerge.setDisplayName(webAppSrc1.getDisplayName()); } else { webAppConflict.setDisplayName("SM-> " + webAppSrc1.getDisplayName() + " :: TS-> " + webAppSrc2.getDisplayName()); } mergeContextParam(webAppSrc1, webAppSrc2, webAppMerge, webAppConflict); mergeFilter(webAppSrc1, webAppSrc2, webAppMerge, webAppConflict); mergeFilterMapping(webAppSrc1, webAppSrc2, webAppMerge, webAppConflict); mergeListener(webAppSrc1, webAppSrc2, webAppMerge, webAppConflict); mergeServlet(webAppSrc1, webAppSrc2, webAppMerge, webAppConflict); mergeServletMapping(webAppSrc1, webAppSrc2, webAppMerge, webAppConflict); serializer.write(webAppMerge, new OutputStreamWriter(new FileOutputStream(destfile), "UTF-8")); if(conflictfile != null && conflictfile.length() > 0) { serializer.write(webAppConflict, new OutputStreamWriter(new FileOutputStream(conflictfile), "UTF-8")); } }
public static Serializer create() { RegistryMatcher m = new RegistryMatcher(); m.bind( org.gedcomx.common.URI.class, UriTransformer.class ); m.bind( Date.class, DateTransformer.class ); return new Persister( m ); }
private SerializeHelper() { matcher = new RegistryMatcher(); serializer = new Persister(new SpeechActStrategy(), matcher); }