Skip to content

Commit

Permalink
Mapsforge themes compatibility improvements opensciencemap#100, fix #392
Browse files Browse the repository at this point in the history
  • Loading branch information
devemux86 committed Sep 5, 2017
1 parent eddfa5a commit 07138f3
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright 2010, 2011, 2012 mapsforge.org
* Copyright 2016 devemux86
* Copyright 2016-2017 devemux86
* Copyright 2017 Longri
*
* This program is free software: you can redistribute it and/or modify it under the
Expand All @@ -27,7 +27,6 @@
import org.xml.sax.helpers.DefaultHandler;

import java.io.File;
import java.io.FileInputStream;

import javax.xml.parsers.SAXParserFactory;

Expand All @@ -43,7 +42,7 @@ public boolean accept(File file) {
try {
ThemeFile theme = new ExternalRenderTheme(file.getAbsolutePath());
DefaultHandler renderThemeHandler;
if (ThemeUtils.isMapsforgeTheme(new FileInputStream(file)))
if (ThemeUtils.isMapsforgeTheme(theme))
renderThemeHandler = new XmlMapsforgeThemeBuilder(theme);
else
renderThemeHandler = new XmlThemeBuilder(theme);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016 devemux86
* Copyright 2016-2017 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
Expand Down Expand Up @@ -58,7 +58,7 @@ public boolean onOptionsItemSelected(MenuItem item) {

@Override
protected void loadTheme(final String styleId) {
mMap.setTheme(new AssetsRenderTheme(this, "", "vtm/stylemenu.xml", new XmlRenderThemeMenuCallback() {
mMap.setTheme(new AssetsRenderTheme(getAssets(), "", "vtm/stylemenu.xml", new XmlRenderThemeMenuCallback() {
@Override
public Set<String> getCategories(XmlRenderThemeStyleMenu renderThemeStyleMenu) {
// Use the selected style or the default
Expand Down
31 changes: 16 additions & 15 deletions vtm-android/src/org/oscim/android/theme/AssetsRenderTheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package org.oscim.android.theme;

import android.content.Context;
import android.content.res.AssetManager;
import android.text.TextUtils;

import org.oscim.theme.IRenderTheme.ThemeException;
Expand All @@ -33,36 +33,33 @@
public class AssetsRenderTheme implements ThemeFile {
private static final long serialVersionUID = 1L;

private final InputStream mInputStream;
private final AssetManager mAssetManager;
private final String mFileName;
private XmlRenderThemeMenuCallback mMenuCallback;
private final String mRelativePathPrefix;

/**
* @param context the Android context.
* @param assetManager the Android asset manager.
* @param relativePathPrefix the prefix for all relative resource paths.
* @param fileName the path to the XML render theme file.
* @throws ThemeException if an error occurs while reading the render theme XML.
*/
public AssetsRenderTheme(Context context, String relativePathPrefix, String fileName) throws ThemeException {
this(context, relativePathPrefix, fileName, null);
public AssetsRenderTheme(AssetManager assetManager, String relativePathPrefix, String fileName) throws ThemeException {
this(assetManager, relativePathPrefix, fileName, null);
}

/**
* @param context the Android context.
* @param assetManager the Android asset manager.
* @param relativePathPrefix the prefix for all relative resource paths.
* @param fileName the path to the XML render theme file.
* @param menuCallback the interface callback to create a settings menu on the fly.
* @throws ThemeException if an error occurs while reading the render theme XML.
*/
public AssetsRenderTheme(Context context, String relativePathPrefix, String fileName, XmlRenderThemeMenuCallback menuCallback) throws ThemeException {
public AssetsRenderTheme(AssetManager assetManager, String relativePathPrefix, String fileName, XmlRenderThemeMenuCallback menuCallback) throws ThemeException {
mAssetManager = assetManager;
mRelativePathPrefix = relativePathPrefix;
mFileName = fileName;
mMenuCallback = menuCallback;

try {
mInputStream = context.getAssets().open((TextUtils.isEmpty(mRelativePathPrefix) ? "" : mRelativePathPrefix) + fileName);
} catch (IOException e) {
throw new ThemeException(e.getMessage());
}
}

@Override
Expand All @@ -73,7 +70,7 @@ public boolean equals(Object obj) {
return false;
}
AssetsRenderTheme other = (AssetsRenderTheme) obj;
if (mInputStream != other.mInputStream) {
if (getRenderThemeAsStream() != other.getRenderThemeAsStream()) {
return false;
}
if (!Utils.equals(mRelativePathPrefix, other.mRelativePathPrefix)) {
Expand All @@ -94,7 +91,11 @@ public String getRelativePathPrefix() {

@Override
public InputStream getRenderThemeAsStream() throws ThemeException {
return mInputStream;
try {
return mAssetManager.open((TextUtils.isEmpty(mRelativePathPrefix) ? "" : mRelativePathPrefix) + mFileName);
} catch (IOException e) {
throw new ThemeException(e.getMessage());
}
}

@Override
Expand Down
10 changes: 9 additions & 1 deletion vtm/src/org/oscim/theme/StreamRenderTheme.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.oscim.theme.IRenderTheme.ThemeException;
import org.oscim.utils.Utils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
Expand Down Expand Up @@ -45,7 +47,8 @@ public StreamRenderTheme(String relativePathPrefix, InputStream inputStream) {
*/
public StreamRenderTheme(String relativePathPrefix, InputStream inputStream, XmlRenderThemeMenuCallback menuCallback) {
mRelativePathPrefix = relativePathPrefix;
mInputStream = inputStream;
mInputStream = new BufferedInputStream(inputStream);
mInputStream.mark(0);
mMenuCallback = menuCallback;
}

Expand Down Expand Up @@ -78,6 +81,11 @@ public String getRelativePathPrefix() {

@Override
public InputStream getRenderThemeAsStream() throws ThemeException {
try {
mInputStream.reset();
} catch (IOException e) {
throw new ThemeException(e.getMessage());
}
return mInputStream;
}

Expand Down
2 changes: 1 addition & 1 deletion vtm/src/org/oscim/theme/ThemeLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public static IRenderTheme load(ThemeFile theme) throws ThemeException {

public static IRenderTheme load(ThemeFile theme, ThemeCallback themeCallback) throws ThemeException {
IRenderTheme t;
if (ThemeUtils.isMapsforgeTheme(theme.getRenderThemeAsStream()))
if (ThemeUtils.isMapsforgeTheme(theme))
t = USE_ATLAS ? XmlMapsforgeAtlasThemeBuilder.read(theme, themeCallback) : XmlMapsforgeThemeBuilder.read(theme, themeCallback);
else
t = USE_ATLAS ? XmlAtlasThemeBuilder.read(theme, themeCallback) : XmlThemeBuilder.read(theme, themeCallback);
Expand Down
35 changes: 12 additions & 23 deletions vtm/src/org/oscim/theme/ThemeUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,15 @@
*/
package org.oscim.theme;

import org.oscim.utils.IOUtils;
import org.oscim.backend.XMLReaderAdapter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

import java.io.InputStream;
import java.util.concurrent.atomic.AtomicBoolean;

import javax.xml.parsers.SAXParserFactory;

/**
* A utility class with theme specific helper methods.
*/
Expand All @@ -37,34 +32,28 @@ public final class ThemeUtils {
private static final Logger log = LoggerFactory.getLogger(ThemeUtils.class);

/**
* Check if the given InputStream is a Mapsforge render theme.
* Check if the given theme is a Mapsforge one.
*/
public static boolean isMapsforgeTheme(InputStream is) {
public static boolean isMapsforgeTheme(ThemeFile theme) {
try {
final AtomicBoolean isMapsforgeTheme = new AtomicBoolean(false);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
XMLReader xmlReader = factory.newSAXParser().getXMLReader();
xmlReader.setContentHandler(new DefaultHandler() {
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (localName.equals("rendertheme")) {
isMapsforgeTheme.set(uri.equals("http://mapsforge.org/renderTheme"));
// We have all info, break parsing
throw new SAXTerminationException();
}
}
});
try {
xmlReader.parse(new InputSource(is));
new XMLReaderAdapter().parse(new DefaultHandler() {
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (localName.equals("rendertheme")) {
isMapsforgeTheme.set(uri.equals("http://mapsforge.org/renderTheme"));
// We have all info, break parsing
throw new SAXTerminationException();
}
}
}, theme.getRenderThemeAsStream());
} catch (SAXTerminationException e) {
// Do nothing
}
return isMapsforgeTheme.get();
} catch (Exception e) {
log.error(e.getMessage(), e);
return false;
} finally {
IOUtils.closeQuietly(is);
}
}

Expand Down

0 comments on commit 07138f3

Please sign in to comment.