Skip to content

Commit

Permalink
Add initFromResources readRenderingConstantsFromIS
Browse files Browse the repository at this point in the history
  • Loading branch information
RZR-UA committed Oct 2, 2024
1 parent 4d3a88f commit bbade0b
Showing 1 changed file with 37 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,31 @@ public String getDependsName() {
public interface RenderingRulesStorageResolver {
RenderingRulesStorage resolve(String name, RenderingRulesStorageResolver ref) throws XmlPullParserException, IOException;
}


public static RenderingRulesStorage initFromResources(String styleName, String resourceName) {
try {
final RenderingRulesStorage storage = new RenderingRulesStorage(styleName,
readRenderingConstantsFromIS(RenderingRulesStorage.class.getResourceAsStream(resourceName)));
final RenderingRulesStorageResolver resolver = (name, ref) -> {
final String resource = name + ".render.xml";
final RenderingRulesStorage depends = new RenderingRulesStorage(name,
readRenderingConstantsFromIS(RenderingRulesStorage.class.getResourceAsStream(resource)));
final InputStream depStream = RenderingRulesStorage.class.getResourceAsStream(resource);
depends.parseRulesFromXmlInputStream(depStream, ref, false);
depStream.close();
return depends;
};
final InputStream xmlStream = RenderingRulesStorage.class.getResourceAsStream(resourceName);
storage.parseRulesFromXmlInputStream(xmlStream, resolver, false);
xmlStream.close();
return storage;
} catch (XmlPullParserException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public RenderingRulesStorage(String name, Map<String, String> renderingConstants){
getDictionaryValue("");
this.renderingName = name;
Expand Down Expand Up @@ -616,26 +640,9 @@ public static void main(String[] args) throws XmlPullParserException, IOExceptio
styleName = "test";
styleFile = new File(stylesDir, styleName +".render.xml");
}
final Map<String, String> renderingConstants = new LinkedHashMap<String, String>();

final Map<String, String> renderingConstants = readRenderingConstantsFromIS(defaultIS);

try {
XmlPullParser parser = PlatformUtil.newXMLPullParser();
parser.setInput(defaultIS, "UTF-8");
int tok;
while ((tok = parser.next()) != XmlPullParser.END_DOCUMENT) {
if (tok == XmlPullParser.START_TAG) {
String tagName = parser.getName();
if (tagName.equals("renderingConstant")) {
if (!renderingConstants.containsKey(parser.getAttributeValue("", "name"))) {
renderingConstants.put(parser.getAttributeValue("", "name"),
parser.getAttributeValue("", "value"));
}
}
}
}
} finally {
defaultIS.close();
}
RenderingRulesStorage storage = new RenderingRulesStorage(styleName, renderingConstants);
final RenderingRulesStorageResolver resolver = new RenderingRulesStorageResolver() {
@Override
Expand Down Expand Up @@ -699,12 +706,8 @@ protected static void testSearch(RenderingRulesStorage storage) {
// System.out.println((System.nanoTime()- tm)/ (1e6f * count) );
}

public static RenderingRulesStorage getTestStorageForStyle(String filePath) throws XmlPullParserException, IOException {
RenderingRulesStorage.STORE_ATTRIBUTES = true;
public static Map<String, String> readRenderingConstantsFromIS(InputStream is) throws XmlPullParserException, IOException {
Map<String, String> renderingConstants = new LinkedHashMap<String, String>();

InputStream is = new FileInputStream(filePath);
// buggy attributes
try {
XmlPullParser parser = PlatformUtil.newXMLPullParser();
parser.setInput(is, "UTF-8");
Expand All @@ -723,6 +726,15 @@ public static RenderingRulesStorage getTestStorageForStyle(String filePath) thro
} finally {
is.close();
}
return renderingConstants;
}

public static RenderingRulesStorage getTestStorageForStyle(String filePath) throws XmlPullParserException, IOException {
RenderingRulesStorage.STORE_ATTRIBUTES = true;

InputStream is = new FileInputStream(filePath);
Map<String, String> renderingConstants = readRenderingConstantsFromIS(is);

is = new FileInputStream(filePath);
RenderingRulesStorage storage = new RenderingRulesStorage("default", renderingConstants);
final RenderingRulesStorageResolver resolver = new RenderingRulesStorageResolver() {
Expand Down

0 comments on commit bbade0b

Please sign in to comment.