/** * Carica il bollettino valanghe. * * @return previsione caricata */ public Bollettino caricaBollettino() { XStream xStream = new XStream(); xStream.ignoreUnknownElements(); xStream.alias("previsione", Bollettino.class); xStream.alias("giorno", Giorno.class); xStream.autodetectAnnotations(true); return loadXml(MeteoSettings.BOLLETTINO_VALANGHE_XML, xStream); }
public document parseXmlDocuments(InputStream is) { // use the magic of serialisation and seralise the xml data to the folder object XStream xstream = new XStream(new DomDriver()); xstream.autodetectAnnotations(true); // set the alias (by default xstream expects all the classes to be in the same package xstream.setClassLoader(document.class.getClassLoader()); xstream.alias("document", document.class); document document = (document) xstream.fromXML(is); return document; }
private cindy.page.power.Power getBasePower(String path) { if (basePoser == null) { XStream xstream = new XStream(new DomDriver()); // 创建XStream 对象 xstream.alias("power", cindy.page.power.Power.class); // 将XML根元素对应类 xstream.autodetectAnnotations(true); try { basePoser = (cindy.page.power.Power) xstream.fromXML(new FileInputStream(new File(path))); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return basePoser; }
public user parseXmlUser(InputStream is) { // use the magic of serialisation and seralise the xml data to the user object XStream xstream = new XStream(new DomDriver()); xstream.autodetectAnnotations(true); // set the alias (by default xstream expects all the classes to be in the same package xstream.alias("user", user.class); xstream.alias("profile", profile.class); xstream.alias("membership", membership.class); xstream.alias("internationalisation", internationalisation.class); xstream.alias("link", link.class); user user = (user) xstream.fromXML(is); return user; }
public folder parseXmlFolder(InputStream is) { // use the magic of serialisation and seralise the xml data to the folder object XStream xstream = new XStream(new DomDriver()); xstream.autodetectAnnotations(true); // set the alias (by default xstream expects all the classes to be in the same package xstream.setClassLoader(folder.class.getClassLoader()); xstream.alias("ancestry", ancestry.class); xstream.alias("actor", actor.class); xstream.alias("folder", folder.class); xstream.alias("folders", folders.class); xstream.alias("documents", documents.class); folder folder = (folder) xstream.fromXML(is); return folder; }
/** * Just creates a fresh XStream instance. * * @return */ public XStream createForXml() { final XStream xstream = new XStream(new XppDriver()); xstream.setMode(XStream.NO_REFERENCES); xstream.autodetectAnnotations(false); return xstream; }
// -------------------------------------------------------------------------- @Override public ITemplateRepository readTemplates(Map<String, Object> globalProperties) throws IOException { this.globalProperties = globalProperties; // Konfiguracja XStream'a: // http://kickjava.com/src/com/thoughtworks/acceptance/MultipleObjectsInOneStreamTest.java.htm XStream xStream = new XStream(); xStream.alias("properties", Map.class); xStream.autodetectAnnotations(true); xStream.processAnnotations( new Class[] { RepositoryBean.class, TemplateBean.class, TemplateParamBean.class, TemplateReferenceBean.class, TemplateResourceBean.class }); xStream.registerConverter(new MapEntryConverter()); xStream.registerConverter(new RepositoryConverter()); // xStream.alias("template", TemplateBean.class); // xStream.useAttributeFor(TemplateBean.class, "id"); // xStream.alias("param", TemplateParamBean.class); // xStream.useAttributeFor(TemplateParamBean.class, "id"); // Budowanie mapy szablonów w oparciu o pliki konfiguracyjne templates.xml // w katalogu konfiguracyjnym aplikacji, katalogu domowym użytkownika oraz // katalogu bieżącym: ITemplateRepository templateRepository = new HashBasedTemplateRepository(); FileInputStream xmlStream = null; // Mapa szablonów globalnych (katalog konfiguracyjny aplikacji): String appSettings = getApplicationSettingsPath(); File appSettingsFile = new File(appSettings); try { xmlStream = new FileInputStream(appSettingsFile); ObjectInputStream in = xStream.createObjectInputStream(new InputStreamReader(xmlStream)); try { while (true) { processObject(Template.Scope.GLOBAL, in.readObject(), templateRepository); } } catch (EOFException e) { System.out.println( "Wczytano ustawienia globalne z pliku " + appSettingsFile.getCanonicalPath()); } } catch (Exception e) { System.err.println("Błąd wczytywania ustawień globalnych: " + e.getMessage()); throw new RuntimeException(e); } finally { if (xmlStream != null) { xmlStream.close(); xmlStream = null; } } // Mapa szablonów użytkownika (katalog domowy użytkownika): String usrSettings = getUserSettingsPath(); File usrSettingsFile = new File(usrSettings); if (usrSettingsFile.exists() && usrSettingsFile.isFile()) { try { xmlStream = new FileInputStream(usrSettingsFile); ObjectInputStream in = xStream.createObjectInputStream(new InputStreamReader(xmlStream)); try { while (true) { processObject(Template.Scope.USER, in.readObject(), templateRepository); } } catch (EOFException e) { System.out.println( "Wczytano ustawienia użytkownika z pliku " + usrSettingsFile.getCanonicalPath()); } } catch (Exception e) { System.err.println("Błąd wczytywania ustawień użytkownika: " + e.getMessage()); throw new RuntimeException(e); } finally { if (xmlStream != null) { xmlStream.close(); xmlStream = null; } } } // Mapa szablonów lokalnych (katalog bieżący projektu): String runSettings = getRunningSettings(); File runSettingsFile = new File(runSettings); if (runSettingsFile.exists() && runSettingsFile.isFile()) { try { xmlStream = new FileInputStream(runSettingsFile); ObjectInputStream in = xStream.createObjectInputStream(new InputStreamReader(xmlStream)); try { while (true) { processObject(Template.Scope.RUNTIME, in.readObject(), templateRepository); } } catch (EOFException e) { System.out.println( "Wczytano ustawienia lokalne z pliku " + runSettingsFile.getCanonicalPath()); } } catch (Exception e) { System.err.println("Błąd wczytywania ustawień lokalnych: " + e.getMessage()); throw new RuntimeException(e); } finally { if (xmlStream != null) { xmlStream.close(); xmlStream = null; } } } return templateRepository; }
static { XSTREAM.autodetectAnnotations(true); XSTREAM.setMode(XStream.NO_REFERENCES); }
/** * Configure the XStream instance with this marshaller's bean properties. * * @param xstream the {@code XStream} instance */ protected void configureXStream(XStream xstream) { if (this.converters != null) { for (int i = 0; i < this.converters.length; i++) { if (this.converters[i] instanceof Converter) { xstream.registerConverter((Converter) this.converters[i], i); } else if (this.converters[i] instanceof SingleValueConverter) { xstream.registerConverter((SingleValueConverter) this.converters[i], i); } else { throw new IllegalArgumentException( "Invalid ConverterMatcher [" + this.converters[i] + "]"); } } } if (this.marshallingStrategy != null) { xstream.setMarshallingStrategy(this.marshallingStrategy); } if (this.mode != null) { xstream.setMode(this.mode); } try { if (this.aliases != null) { Map<String, Class<?>> classMap = toClassMap(this.aliases); for (Map.Entry<String, Class<?>> entry : classMap.entrySet()) { xstream.alias(entry.getKey(), entry.getValue()); } } if (this.aliasesByType != null) { Map<String, Class<?>> classMap = toClassMap(this.aliasesByType); for (Map.Entry<String, Class<?>> entry : classMap.entrySet()) { xstream.aliasType(entry.getKey(), entry.getValue()); } } if (this.fieldAliases != null) { for (Map.Entry<String, String> entry : this.fieldAliases.entrySet()) { String alias = entry.getValue(); String field = entry.getKey(); int idx = field.lastIndexOf('.'); if (idx != -1) { String className = field.substring(0, idx); Class<?> clazz = ClassUtils.forName(className, this.beanClassLoader); String fieldName = field.substring(idx + 1); xstream.aliasField(alias, clazz, fieldName); } else { throw new IllegalArgumentException("Field name [" + field + "] does not contain '.'"); } } } } catch (ClassNotFoundException ex) { throw new IllegalStateException("Failed to load specified alias class", ex); } if (this.useAttributeForTypes != null) { for (Class<?> type : this.useAttributeForTypes) { xstream.useAttributeFor(type); } } if (this.useAttributeFor != null) { for (Map.Entry<?, ?> entry : this.useAttributeFor.entrySet()) { if (entry.getKey() instanceof String) { if (entry.getValue() instanceof Class) { xstream.useAttributeFor((String) entry.getKey(), (Class<?>) entry.getValue()); } else { throw new IllegalArgumentException( "'useAttributesFor' takes Map<String, Class> when using a map key of type String"); } } else if (entry.getKey() instanceof Class) { Class<?> key = (Class<?>) entry.getKey(); if (entry.getValue() instanceof String) { xstream.useAttributeFor(key, (String) entry.getValue()); } else if (entry.getValue() instanceof List) { @SuppressWarnings("unchecked") List<Object> listValue = (List<Object>) entry.getValue(); for (Object element : listValue) { if (element instanceof String) { xstream.useAttributeFor(key, (String) element); } } } else { throw new IllegalArgumentException( "'useAttributesFor' property takes either Map<Class, String> " + "or Map<Class, List<String>> when using a map key of type Class"); } } else { throw new IllegalArgumentException( "'useAttributesFor' property takes either a map key of type String or Class"); } } } if (this.implicitCollections != null) { for (Map.Entry<Class<?>, String> entry : this.implicitCollections.entrySet()) { String[] collectionFields = StringUtils.commaDelimitedListToStringArray(entry.getValue()); for (String collectionField : collectionFields) { xstream.addImplicitCollection(entry.getKey(), collectionField); } } } if (this.omittedFields != null) { for (Map.Entry<Class<?>, String> entry : this.omittedFields.entrySet()) { String[] fields = StringUtils.commaDelimitedListToStringArray(entry.getValue()); for (String field : fields) { xstream.omitField(entry.getKey(), field); } } } if (this.annotatedClasses != null) { xstream.processAnnotations(this.annotatedClasses); } if (this.autodetectAnnotations) { xstream.autodetectAnnotations(true); } }
/** * Configures XStream mapping of Response object with SvaModel content. * * @param xstream XStream */ private static void configure(XStream xstream) { xstream.autodetectAnnotations(false); xstream.alias("response", Response.class); xstream.alias("TaskModel", TaskModel.class); }
/** * REST API Response Representation wrapper for single or multiple items expexted * * @param media MediaType expected * @param representation service response representation * @param dataClass class expected for items of the Response object * @param isArray if true wrap the data property else wrap the item property * @return Response */ public static Response getResponseOrderandUserstorage( MediaType media, Representation representation, Class<?> dataClass, boolean isArray) { try { if (!media.isCompatible(MediaType.APPLICATION_JSON) && !media.isCompatible(MediaType.APPLICATION_XML)) { Engine.getLogger(AbstractSitoolsTestCase.class.getName()) .warning("Only JSON or XML supported in tests"); return null; } XStream xstream = XStreamFactory.getInstance().getXStreamReader(media); xstream.autodetectAnnotations(false); xstream.alias("response", Response.class); // for order xstream.alias("order", Order.class); // for userstorage xstream.alias("userstorage", UserStorage.class); xstream.alias("diskStorage", DiskStorage.class); if (isArray) { xstream.alias("item", dataClass); xstream.alias("item", Object.class, dataClass); // xstream.omitField(Response.class, "data"); if (media.isCompatible(MediaType.APPLICATION_JSON)) { xstream.addImplicitCollection(Response.class, "data", dataClass); xstream.addImplicitCollection(Order.class, "events", Event.class); xstream.addImplicitCollection(Order.class, "resourceCollection", String.class); } } else { xstream.alias("item", dataClass); xstream.alias("item", Object.class, dataClass); if (dataClass == Order.class) { xstream.aliasField("order", Response.class, "item"); if (media.isCompatible(MediaType.APPLICATION_JSON)) { xstream.addImplicitCollection(Order.class, "events", Event.class); xstream.addImplicitCollection(Order.class, "resourceCollection", String.class); } } if (dataClass == UserStorage.class) { xstream.aliasField("userstorage", Response.class, "item"); } } xstream.aliasField("data", Response.class, "data"); SitoolsXStreamRepresentation<Response> rep = new SitoolsXStreamRepresentation<Response>(representation); rep.setXstream(xstream); if (media.isCompatible(getMediaTest())) { Response response = rep.getObject("response"); return response; } else { Engine.getLogger(AbstractSitoolsTestCase.class.getName()) .warning("Only JSON or XML supported in tests"); return null; // TODO complete test with ObjectRepresentation } } finally { RIAPUtils.exhaust(representation); } }
@Override public String atomBuilder(final Upload upload) { // create atom xml metadata - create object first, then convert with // xstream final Metadata metadata = upload.getMetadata(); final VideoEntry videoEntry = new VideoEntry(); videoEntry.mediaGroup.category = new ArrayList<>(1); videoEntry.mediaGroup.category.add(metadata.getCategory().toCategory()); videoEntry.mediaGroup.license = metadata.getLicense().getMetaIdentifier(); videoEntry.mediaGroup.title = metadata.getTitle(); videoEntry.mediaGroup.description = metadata.getDescription(); videoEntry.mediaGroup.keywords = Joiner.on(TagParser.TAG_DELIMITER) .skipNulls() .join(TagParser.parse(metadata.getKeywords())); final Permissions permissions = upload.getPermissions(); if (Visibility.PRIVATE == permissions.getVisibility() || Visibility.SCHEDULED == permissions.getVisibility()) { videoEntry.mediaGroup.ytPrivate = new Object(); } videoEntry.accessControl.add( new YoutubeAccessControl( "embed", PermissionStringConverter.convertBoolean(permissions.isEmbed()))); videoEntry.accessControl.add( new YoutubeAccessControl( "rate", PermissionStringConverter.convertBoolean(permissions.isRate()))); videoEntry.accessControl.add( new YoutubeAccessControl( "commentVote", PermissionStringConverter.convertBoolean(permissions.isCommentvote()))); videoEntry.accessControl.add( new YoutubeAccessControl( "comment", PermissionStringConverter.convertInteger(permissions.getComment().ordinal()))); videoEntry.accessControl.add( new YoutubeAccessControl( "list", PermissionStringConverter.convertBoolean( Visibility.PUBLIC == permissions.getVisibility()))); // convert metadata with xstream final XStream xStream = new XStream( new DomDriver(Charsets.UTF_8.name()) { @Override public HierarchicalStreamWriter createWriter(final Writer out) { return new PrettyPrintWriter(out) { boolean isCDATA; @Override public void startNode( final String name, @SuppressWarnings("rawtypes") final Class clazz) { super.startNode(name, clazz); isCDATA = "media:description".equals(name) || "media:keywords".equals(name) || "media:title".equals(name); } @Override protected void writeText(final QuickWriter writer, String text) { final String tmpText = Strings.nullToEmpty(text); text = "null".equals(tmpText) ? "" : tmpText; if (isCDATA) { writer.write("<![CDATA["); writer.write(text); writer.write("]]>"); } else { super.writeText(writer, text); } } }; } }); xStream.autodetectAnnotations(true); return String.format("<?xml version=\"1.0\" encoding=\"UTF-8\"?>%s", xStream.toXML(videoEntry)); }