Workspace { public void parseXML(List<RemoteMapContext> mapContextList,XMLStreamReader parser) throws XMLStreamException, UnsupportedEncodingException { List<String> hierarchy = new ArrayList<String>(); RemoteMapContext curMapContext = null; Locale curLocale = null; StringBuilder characters = new StringBuilder(); for (int event = parser.next(); event != XMLStreamConstants.END_DOCUMENT; event = parser.next()) { switch(event) { case XMLStreamConstants.START_ELEMENT: hierarchy.add(parser.getLocalName()); if(RemoteCommons.endsWith(hierarchy,"contexts","context")) { curMapContext = new RemoteOwsMapContext(cParams); curMapContext.setWorkspaceName(workspaceName); } for (int attributeId = 0; attributeId < parser.getAttributeCount(); attributeId++) { String attributeName = parser.getAttributeLocalName(attributeId); if (attributeName.equals("id")) { curMapContext.setId(Integer.parseInt(parser.getAttributeValue(attributeId))); } else if (attributeName.equals("date")) { String attributeValue = parser.getAttributeValue(attributeId); try { curMapContext.setDate(parseDate(attributeValue)); } catch (ParseException ex) { LOGGER.warn(I18N.tr("Cannot parse the provided date {0}",attributeValue),ex); } } else if (attributeName.equals("lang")) { curLocale=LocalizedText.forLanguageTag(parser.getAttributeValue(attributeId)); } } break; case XMLStreamConstants.END_ELEMENT: if(RemoteCommons.endsWith(hierarchy,"contexts","context")) { mapContextList.add(curMapContext); curMapContext = null; } else if (RemoteCommons.endsWith(hierarchy,"contexts","context","title")) { Locale descLocale = Locale.getDefault(); if(curLocale!=null) { descLocale = curLocale; } curMapContext.getDescription().addTitle(descLocale,StringEscapeUtils.unescapeHtml4(characters.toString().trim())); } else if(RemoteCommons.endsWith(hierarchy,"contexts","context","abstract")) { Locale descLocale = Locale.getDefault(); if(curLocale!=null) { descLocale = curLocale; } curMapContext.getDescription().addAbstract(descLocale, StringEscapeUtils.unescapeHtml4(characters.toString().trim())); } characters = new StringBuilder(); curLocale = null; hierarchy.remove(hierarchy.size()-1); break; case XMLStreamConstants.CHARACTERS: characters.append(StringEscapeUtils.unescapeHtml4(parser.getText())); break; } } } Workspace(ConnectionProperties cParams, String workspaceName); static Date parseDate(String dateStr); int publishMapContext(MapContext mapContext, Integer mapContextId); void parseXML(List<RemoteMapContext> mapContextList,XMLStreamReader parser); String getWorkspaceName(); List<RemoteMapContext> getMapContextList();  }
@Test public void testParseXML() throws Exception { URL apiUrl = new URL("http: ConnectionProperties cParams = new ConnectionProperties(apiUrl, null,new File("target/maps")); FileReader inFile = new FileReader(new File(RemoteMapCatalogTest.class.getResource("context.xml").toURI())); BufferedReader in = new BufferedReader(inFile); XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader parser; parser = factory.createXMLStreamReader(in); Workspace mapCatalog = new Workspace(cParams, "default"); List<RemoteMapContext> context = new ArrayList<RemoteMapContext>(); mapCatalog.parseXML(context, parser); parser.close(); assertEquals(context.size(), 2); assertEquals(context.get(0).getDescription().getTitle(Locale.ENGLISH), "My custom context"); assertEquals(context.get(0).getDescription().getAbstract(Locale.ENGLISH), "Some text here."); assertEquals(context.get(0).getDate(), Workspace.parseDate("2012.10.12 15:13:29 CEST")); assertEquals(context.get(0).getId(), 0); assertEquals(context.get(1).getDescription().getDefaultTitle(), "Représentation par diagrammes"); assertEquals(context.get(1).getDate(), Workspace.parseDate("2012.10.12 15:13:34 CEST")); assertEquals(context.get(1).getId(), 1); }