Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tool for generating JSON from DDF list file
Signed-off-by: Ville Skyttä <ville.skytta@iki.fi>
- Loading branch information
1 parent
a801ddc
commit cfaac6b
Showing
2 changed files
with
131 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
leshan-core/src/main/java/org/eclipse/leshan/core/model/DdfList2JsonGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2013-2017 Sierra Wireless and others. | ||
* | ||
* All rights reserved. This program and the accompanying materials | ||
* are made available under the terms of the Eclipse Public License v1.0 | ||
* and Eclipse Distribution License v1.0 which accompany this distribution. | ||
* | ||
* The Eclipse Public License is available at | ||
* http://www.eclipse.org/legal/epl-v10.html | ||
* and the Eclipse Distribution License is available at | ||
* http://www.eclipse.org/org/documents/edl-v10.html. | ||
* | ||
* Contributors: | ||
* Ville Skyttä - initial implementation | ||
*******************************************************************************/ | ||
package org.eclipse.leshan.core.model; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.net.URLConnection; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.xml.parsers.DocumentBuilder; | ||
import javax.xml.parsers.DocumentBuilderFactory; | ||
import javax.xml.parsers.ParserConfigurationException; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.w3c.dom.Document; | ||
import org.w3c.dom.Element; | ||
import org.w3c.dom.Node; | ||
import org.w3c.dom.NodeList; | ||
import org.xml.sax.SAXException; | ||
|
||
public class DdfList2JsonGenerator { | ||
|
||
private static final Logger LOG = LoggerFactory.getLogger(DdfList2JsonGenerator.class); | ||
|
||
private final DocumentBuilderFactory factory; | ||
|
||
public DdfList2JsonGenerator() { | ||
factory = DocumentBuilderFactory.newInstance(); | ||
} | ||
|
||
private void processDdfList(String url, String ddfFilesPath) throws IOException { | ||
|
||
LOG.debug("Processing DDF list file {}", url); | ||
|
||
List<String> ddfUrls = new ArrayList<>(); | ||
|
||
try { | ||
DocumentBuilder builder = factory.newDocumentBuilder(); | ||
Document document = builder.parse(url); | ||
|
||
NodeList items = document.getDocumentElement().getElementsByTagName("Item"); | ||
for (int i = 0; i < items.getLength(); i++) { | ||
Node item = items.item(i); | ||
Node ddf = ((Element) item).getElementsByTagName("DDF").item(0); | ||
ddfUrls.add(ddf.getTextContent()); | ||
} | ||
} catch (SAXException | ParserConfigurationException e) { | ||
throw new IOException(e); | ||
} | ||
|
||
for (String ddfUrl : ddfUrls) { | ||
|
||
URL parsedUrl; | ||
try { | ||
parsedUrl = new URL(ddfUrl); | ||
} catch (MalformedURLException e) { | ||
LOG.error("Skipping malformed URL {}", ddfUrl); | ||
continue; | ||
} | ||
|
||
String filename = parsedUrl.getPath(); | ||
filename = filename.substring(filename.lastIndexOf("/")); | ||
|
||
Path outPath = Paths.get(ddfFilesPath, filename); | ||
|
||
LOG.debug("Downloading DDF file {} to {}", ddfUrl, outPath); | ||
|
||
URLConnection conn = parsedUrl.openConnection(); | ||
// The default Java User-Agent gets 403 Forbidden from OMA website | ||
conn.setRequestProperty("User-Agent", "Leshan " + getClass().getSimpleName()); | ||
|
||
try (InputStream in = conn.getInputStream()) { | ||
Files.copy(in, outPath); | ||
} | ||
} | ||
} | ||
|
||
public static void main(String[] args) throws IOException { | ||
// default values | ||
String ddfFilesPath = Ddf2JsonGenerator.DEFAULT_DDF_FILES_PATH; | ||
String outputPath = Ddf2JsonGenerator.DEFAULT_OUTPUT_PATH; | ||
String ddfListUrl = "https://raw.githubusercontent.com/OpenMobileAlliance/openmobilealliance.github.io/master/OMNA/LwM2M/DDF.xml"; | ||
|
||
// use arguments if they exist | ||
if (args.length >= 1) | ||
ddfFilesPath = args[0]; // the path to a DDF file or a folder which contains DDF files. | ||
if (args.length >= 2) | ||
outputPath = args[1]; // the path of the output file. | ||
if (args.length >= 3) | ||
ddfListUrl = args[2]; // the path of the DDF list file. | ||
|
||
new DdfList2JsonGenerator().processDdfList(ddfListUrl, ddfFilesPath); | ||
|
||
LOG.error("DDF list {} processed to {}, proceeding with JSON generation in {}", ddfListUrl, ddfFilesPath, | ||
outputPath); | ||
|
||
// generate object spec file | ||
Ddf2JsonGenerator ddfJsonGenerator = new Ddf2JsonGenerator(); | ||
try (FileOutputStream fileOutputStream = new FileOutputStream(outputPath)) { | ||
ddfJsonGenerator.generate(new File(ddfFilesPath), fileOutputStream); | ||
} | ||
} | ||
} |