reduce the exceptions thrown by theme loader to ThemeException
This commit is contained in:
parent
6f9d2dafb6
commit
2fd9addc59
@ -20,11 +20,9 @@ package org.oscim.theme;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.ObjectInputStream;
|
|
||||||
|
|
||||||
import org.oscim.backend.AssetAdapter;
|
import org.oscim.theme.IRenderTheme.ThemeException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An ExternalRenderTheme allows for customizing the rendering style of the map
|
* An ExternalRenderTheme allows for customizing the rendering style of the map
|
||||||
@ -34,39 +32,30 @@ public class ExternalRenderTheme implements ThemeFile {
|
|||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
||||||
private final long mFileModificationDate;
|
private final long mFileModificationDate;
|
||||||
private transient int mHashCodeValue;
|
private final String mPath;
|
||||||
private final String mRenderThemePath;
|
|
||||||
|
|
||||||
private InputStream mInputStream;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param renderThemePath
|
* @param fileName
|
||||||
* the path to the XML render theme file.
|
* the path to the XML render theme file.
|
||||||
* @throws FileNotFoundException
|
* @throws FileNotFoundException
|
||||||
* if the file does not exist or cannot be read.
|
* if the file does not exist or cannot be read.
|
||||||
*/
|
*/
|
||||||
public ExternalRenderTheme(String renderThemePath) throws FileNotFoundException {
|
public ExternalRenderTheme(String fileName) {
|
||||||
mInputStream = AssetAdapter.g.openFileAsStream(renderThemePath);
|
|
||||||
if (mInputStream != null) {
|
File themeFile = new File(fileName);
|
||||||
mRenderThemePath = null;
|
if (!themeFile.exists()) {
|
||||||
mFileModificationDate = 0;
|
throw new ThemeException("file does not exist: " + fileName);
|
||||||
return;
|
} else if (!themeFile.isFile()) {
|
||||||
}
|
throw new ThemeException("not a file: " + fileName);
|
||||||
File renderThemeFile = new File(renderThemePath);
|
} else if (!themeFile.canRead()) {
|
||||||
if (!renderThemeFile.exists()) {
|
throw new ThemeException("cannot read file: " + fileName);
|
||||||
throw new FileNotFoundException("file does not exist: " + renderThemePath);
|
|
||||||
} else if (!renderThemeFile.isFile()) {
|
|
||||||
throw new FileNotFoundException("not a file: " + renderThemePath);
|
|
||||||
} else if (!renderThemeFile.canRead()) {
|
|
||||||
throw new FileNotFoundException("cannot read file: " + renderThemePath);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
mFileModificationDate = renderThemeFile.lastModified();
|
mFileModificationDate = themeFile.lastModified();
|
||||||
if (mFileModificationDate == 0L) {
|
if (mFileModificationDate == 0L) {
|
||||||
throw new FileNotFoundException("cannot read last modification time");
|
throw new ThemeException("cannot read last modification time");
|
||||||
}
|
}
|
||||||
mRenderThemePath = renderThemePath;
|
mPath = fileName;
|
||||||
calculateTransientValues();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -79,47 +68,23 @@ public class ExternalRenderTheme implements ThemeFile {
|
|||||||
ExternalRenderTheme other = (ExternalRenderTheme) obj;
|
ExternalRenderTheme other = (ExternalRenderTheme) obj;
|
||||||
if (mFileModificationDate != other.mFileModificationDate) {
|
if (mFileModificationDate != other.mFileModificationDate) {
|
||||||
return false;
|
return false;
|
||||||
} else if (mRenderThemePath == null && other.mRenderThemePath != null) {
|
} else if (mPath == null && other.mPath != null) {
|
||||||
return false;
|
return false;
|
||||||
} else if (mRenderThemePath != null && !mRenderThemePath.equals(other.mRenderThemePath)) {
|
} else if (mPath != null && !mPath.equals(other.mPath)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public InputStream getRenderThemeAsStream() throws FileNotFoundException {
|
public InputStream getRenderThemeAsStream() {
|
||||||
if (mInputStream != null)
|
InputStream is;
|
||||||
return mInputStream;
|
|
||||||
|
|
||||||
return new FileInputStream(mRenderThemePath);
|
try {
|
||||||
|
is = new FileInputStream(mPath);
|
||||||
|
} catch (FileNotFoundException e) {
|
||||||
|
throw new ThemeException(e.getMessage());
|
||||||
}
|
}
|
||||||
|
return is;
|
||||||
@Override
|
|
||||||
public int hashCode() {
|
|
||||||
return mHashCodeValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return the hash code of this object.
|
|
||||||
*/
|
|
||||||
private int calculateHashCode() {
|
|
||||||
int result = 1;
|
|
||||||
result = 31 * result + (int) (mFileModificationDate ^ (mFileModificationDate >>> 32));
|
|
||||||
result = 31 * result + ((mRenderThemePath == null) ? 0 : mRenderThemePath.hashCode());
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Calculates the values of some transient variables.
|
|
||||||
*/
|
|
||||||
private void calculateTransientValues() {
|
|
||||||
mHashCodeValue = calculateHashCode();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void readObject(ObjectInputStream objectInputStream) throws IOException,
|
|
||||||
ClassNotFoundException {
|
|
||||||
objectInputStream.defaultReadObject();
|
|
||||||
calculateTransientValues();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -18,10 +18,10 @@
|
|||||||
package org.oscim.theme;
|
package org.oscim.theme;
|
||||||
|
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
import org.oscim.backend.CanvasAdapter;
|
import org.oscim.backend.CanvasAdapter;
|
||||||
|
import org.oscim.theme.IRenderTheme.ThemeException;
|
||||||
import org.oscim.utils.IOUtils;
|
import org.oscim.utils.IOUtils;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
@ -32,15 +32,15 @@ public class ThemeLoader {
|
|||||||
/**
|
/**
|
||||||
* Load theme from XML file.
|
* Load theme from XML file.
|
||||||
*
|
*
|
||||||
* @param renderThemePath ..
|
* @throws FileNotFoundException
|
||||||
* @return ...
|
* @throws ThemeException
|
||||||
* @throws FileNotFoundException ...
|
|
||||||
*/
|
*/
|
||||||
public static IRenderTheme load(String renderThemePath) throws FileNotFoundException {
|
public static IRenderTheme load(String renderThemePath) throws ThemeException,
|
||||||
|
FileNotFoundException {
|
||||||
return load(new ExternalRenderTheme(renderThemePath));
|
return load(new ExternalRenderTheme(renderThemePath));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IRenderTheme load(ThemeFile theme) {
|
public static IRenderTheme load(ThemeFile theme) throws ThemeException {
|
||||||
|
|
||||||
InputStream inputStream = null;
|
InputStream inputStream = null;
|
||||||
try {
|
try {
|
||||||
@ -51,11 +51,8 @@ public class ThemeLoader {
|
|||||||
t.scaleTextSize(CanvasAdapter.textScale + (CanvasAdapter.dpi / 240 - 1) * 0.5f);
|
t.scaleTextSize(CanvasAdapter.textScale + (CanvasAdapter.dpi / 240 - 1) * 0.5f);
|
||||||
|
|
||||||
return t;
|
return t;
|
||||||
} catch (IOException e) {
|
} catch (FileNotFoundException e) {
|
||||||
log.error(e.getMessage());
|
log.error(e.getMessage());
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
|
|
||||||
} finally {
|
} finally {
|
||||||
IOUtils.closeQuietly(inputStream);
|
IOUtils.closeQuietly(inputStream);
|
||||||
}
|
}
|
||||||
|
@ -84,17 +84,21 @@ public class XmlThemeBuilder extends DefaultHandler {
|
|||||||
* an input stream containing valid render theme XML data.
|
* an input stream containing valid render theme XML data.
|
||||||
* @return a new RenderTheme which is created by parsing the XML data from
|
* @return a new RenderTheme which is created by parsing the XML data from
|
||||||
* the input stream.
|
* the input stream.
|
||||||
* @throws SAXException
|
* @throws ThemeException
|
||||||
* if an error occurs while parsing the render theme XML.
|
* if an error occurs while parsing the render theme XML.
|
||||||
* @throws IOException
|
* @throws IOException
|
||||||
* if an I/O error occurs while reading from the input stream.
|
* if an I/O error occurs while reading from the input stream.
|
||||||
*/
|
*/
|
||||||
public static IRenderTheme read(InputStream inputStream)
|
public static IRenderTheme read(InputStream inputStream)
|
||||||
throws SAXException, IOException {
|
throws ThemeException {
|
||||||
|
|
||||||
XmlThemeBuilder renderThemeHandler = new XmlThemeBuilder();
|
XmlThemeBuilder renderThemeHandler = new XmlThemeBuilder();
|
||||||
|
|
||||||
|
try {
|
||||||
new XMLReaderAdapter().parse(renderThemeHandler, inputStream);
|
new XMLReaderAdapter().parse(renderThemeHandler, inputStream);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new ThemeException(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
return renderThemeHandler.mRenderTheme;
|
return renderThemeHandler.mRenderTheme;
|
||||||
}
|
}
|
||||||
@ -187,7 +191,7 @@ public class XmlThemeBuilder extends DefaultHandler {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void startElement(String uri, String localName, String qName,
|
public void startElement(String uri, String localName, String qName,
|
||||||
Attributes attributes) throws SAXException {
|
Attributes attributes) throws ThemeException {
|
||||||
try {
|
try {
|
||||||
if (ELEMENT_NAME_RENDER_THEME.equals(localName)) {
|
if (ELEMENT_NAME_RENDER_THEME.equals(localName)) {
|
||||||
checkState(localName, Element.RENDER_THEME);
|
checkState(localName, Element.RENDER_THEME);
|
||||||
@ -281,10 +285,10 @@ public class XmlThemeBuilder extends DefaultHandler {
|
|||||||
log.error("unknown element: {}", localName);
|
log.error("unknown element: {}", localName);
|
||||||
//throw new SAXException("unknown element: " + localName);
|
//throw new SAXException("unknown element: " + localName);
|
||||||
}
|
}
|
||||||
} catch (ThemeException e) {
|
} catch (SAXException e) {
|
||||||
throw new SAXException(null, e);
|
throw new ThemeException(e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
throw new SAXException(null, e);
|
throw new ThemeException(e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -361,16 +365,16 @@ public class XmlThemeBuilder extends DefaultHandler {
|
|||||||
b.color(value);
|
b.color(value);
|
||||||
|
|
||||||
else if ("width".equals(name) || "stroke-width".equals(name)) {
|
else if ("width".equals(name) || "stroke-width".equals(name)) {
|
||||||
float width = parseFloat(value);
|
b.width = parseFloat(value);
|
||||||
if (line == null) {
|
if (line == null) {
|
||||||
validateNonNegative("width", width);
|
if (!isOutline)
|
||||||
|
validateNonNegative("width", b.width);
|
||||||
} else {
|
} else {
|
||||||
/* use stroke width relative to 'line' */
|
/* use stroke width relative to 'line' */
|
||||||
width += line.width;
|
b.width += line.width;
|
||||||
if (width <= 0)
|
if (b.width <= 0)
|
||||||
width = 1;
|
b.width = 1;
|
||||||
}
|
}
|
||||||
b.width = width;
|
|
||||||
}
|
}
|
||||||
else if ("cap".equals(name) || "stroke-linecap".equals(name))
|
else if ("cap".equals(name) || "stroke-linecap".equals(name))
|
||||||
b.cap = Cap.valueOf(value.toUpperCase());
|
b.cap = Cap.valueOf(value.toUpperCase());
|
||||||
|
Loading…
x
Reference in New Issue
Block a user