class LwM2MImporter {

	public byte[] convert(InputStream fileInput) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		ZipOutputStream zip = new ZipOutputStream(baos);

		try {
			LWM2M lwm2m = parse(fileInput);

			try {
				for (LWM2M.Object obj : lwm2m.getObject()) {

					final ModelId modelId = createModelId(obj);

					ZipEntry fbEntry = new ZipEntry(modelId.getName() + ".fbmodel");
					zip.putNextEntry(fbEntry);
					zip.write(FB_TEMPLATE.create(obj, modelId).getBytes());
					zip.closeEntry();

					ZipEntry mappingEntry = new ZipEntry(modelId.getName() + ".mapping");
					zip.putNextEntry(mappingEntry);
					zip.write(MAPPING_TEMPLATE.create(obj, modelId).getBytes());
					zip.closeEntry();
				}
			} catch (Exception ex) {
				ex.printStackTrace();
			} finally {
				if (zip != null) {
					zip.close();
				}
			}

			return baos.toByteArray();
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			if (baos != null) {
				try {
					baos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}

}

class LwM2MImporterTest {

  @Test
  public void testConversion()throws Exception {

    final String filename = "3328_1_1.xml";
    
    InputStream lwm2mXml = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);    
    
    LwM2MImporter importer = new LwM2MImporter();
    
    byte[] convertedContent = importer.convert(lwm2mXml);
    
    ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(convertedContent));
    ZipEntry entry = null;
    
    Map<String,byte[]> content = new HashMap<>();
    while ((entry = zis.getNextEntry()) != null) {
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      IOUtils.copy(zis, baos);
      content.put(entry.getName(),baos.toByteArray());
    }
    
    assertEquals(2,content.keySet().size());
    assertTrue(content.keySet().contains("Power.fbmodel"));
    assertTrue(content.keySet().contains("Power.mapping"));
    
    System.out.println(new String(content.get("Power.fbmodel")));
    System.out.println(new String(content.get("Power.mapping")));
  }

}