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