OCVHaarLoader { static OCVHaarClassifierNode readXPP(InputStream in) throws IOException { try { final XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); final XmlPullParser reader = factory.newPullParser(); reader.setInput(in, null); reader.nextTag(); checkNode(reader, OCV_STORAGE_NODE); reader.nextTag(); if (!"opencv-haar-classifier".equals(reader.getAttributeValue(null, "type_id"))) { throw new IOException("Unsupported format: " + reader.getAttributeValue(null, "type_id")); } final OCVHaarClassifierNode root = new OCVHaarClassifierNode(); root.name = reader.getName(); reader.nextTag(); checkNode(reader, SIZE_NODE); final String sizeStr = reader.nextText(); final String[] widthHeight = sizeStr.trim().split(" "); if (widthHeight.length != 2) { throw new IOException("expecting 'w h' for size element, got: " + sizeStr); } root.width = Integer.parseInt(widthHeight[0]); root.height = Integer.parseInt(widthHeight[1]); reader.nextTag(); checkNode(reader, STAGES_NODE); while (reader.nextTag() == XmlPullParser.START_TAG) { checkNode(reader, ANONYMOUS_NODE); final StageNode currentStage = new StageNode(); root.stages.add(currentStage); reader.nextTag(); checkNode(reader, TREES_NODE); while (reader.nextTag() == XmlPullParser.START_TAG) { checkNode(reader, ANONYMOUS_NODE); final List<TreeNode> currentTree = new ArrayList<TreeNode>(); currentStage.trees.add(currentTree); while (reader.nextTag() == XmlPullParser.START_TAG) { checkNode(reader, ANONYMOUS_NODE); final List<WeightedRectangle> regions = new ArrayList<WeightedRectangle>(3); reader.nextTag(); checkNode(reader, FEATURE_NODE); reader.nextTag(); checkNode(reader, RECTS_NODE); while (reader.nextTag() == XmlPullParser.START_TAG) { checkNode(reader, ANONYMOUS_NODE); regions.add(WeightedRectangle.parse(reader.nextText())); } reader.nextTag(); checkNode(reader, TILTED_NODE); final boolean tilted = "1".equals(reader.nextText()); if (tilted) root.hasTiltedFeatures = true; reader.nextTag(); checkNode(reader, FEATURE_NODE); final HaarFeature currentFeature = HaarFeature.create(regions, tilted); reader.nextTag(); checkNode(reader, THRESHOLD_NODE); final float threshold = (float) Double.parseDouble(reader.nextText()); final TreeNode treeNode = new TreeNode(); treeNode.threshold = threshold; treeNode.feature = currentFeature; reader.nextTag(); checkNode(reader, LEFT_VAL_NODE, LEFT_NODE_NODE); final String leftText = reader.nextText(); if ("left_val".equals(reader.getName())) { treeNode.left_val = Float.parseFloat(leftText); } else { treeNode.left_node = Integer.parseInt(leftText); } reader.nextTag(); checkNode(reader, RIGHT_VAL_NODE, RIGHT_NODE_NODE); final String rightText = reader.nextText(); if ("right_val".equals(reader.getName())) { treeNode.right_val = Float.parseFloat(rightText); } else { treeNode.right_node = Integer.parseInt(rightText); } reader.nextTag(); checkNode(reader, ANONYMOUS_NODE); currentTree.add(treeNode); } } reader.nextTag(); checkNode(reader, STAGE_THRESHOLD_NODE); currentStage.threshold = Float.parseFloat(reader.nextText()) - ICV_STAGE_THRESHOLD_BIAS; reader.nextTag(); checkNode(reader, PARENT_NODE); currentStage.parent = Integer.parseInt(reader.nextText()); reader.nextTag(); checkNode(reader, NEXT_NODE); currentStage.next = Integer.parseInt(reader.nextText()); reader.nextTag(); checkNode(reader, ANONYMOUS_NODE); } return root; } catch (final XmlPullParserException ex) { throw new IOException(ex); } }  static StageTreeClassifier read(InputStream is);  }
@Test public void testReadNode() throws IOException { for (final String c : goodCascades) { OCVHaarLoader.readXPP(OCVHaarLoader.class.getResourceAsStream(c)); } }
@Test(expected = IOException.class) public void testReadNodeBad() throws IOException { OCVHaarLoader.readXPP(OCVHaarLoader.class.getResourceAsStream(badCascade)); }