/** * Recursively build the structure hierarchy * * @param deque the deque of nodes * @param s the current annotation */ private static <T extends Annotation> void build(Deque<AnnotationNode<T>> deque, T s) { final AnnotationNode<T> parent = getParent(deque, s); final AnnotationNode<T> node = new AnnotationNode<>(parent, s); parent.addChild(node); deque.push(node); }
private void print(int level, StringBuilder sb, Node<Structure> n) { startTag(level, sb, n.getItem()); int offset = 0; for (final Node<Structure> child : n.getChildren()) { addText( level + 1, sb, n.getItem(), offset, AnnotationNode.getBegin(child) - AnnotationNode.getBegin(n)); print(level + 1, sb, child); offset = AnnotationNode.getEnd(child) - AnnotationNode.getBegin(n); } addText( level + 1, sb, n.getItem(), offset, AnnotationNode.getEnd(n) - AnnotationNode.getBegin(n)); endTag(level, sb, n.getItem()); }
@Override public Map<String, String> attributes() { if (attributes != null) { return attributes; } attributes = new HashMap<>(); T annotation = getItem(); if (annotation != null) { if (annotation instanceof Structure) { addAttribute(attributes, "class", ((Structure) annotation).getElementClass()); addAttribute(attributes, "id", ((Structure) annotation).getElementId()); } for (Method method : annotation.getClass().getDeclaredMethods()) { String name = method.getName(); if (name.startsWith("get") && method.getParameterTypes().length == 0 && !nonAttributes.contains(name)) { String key = name.substring(3).toLowerCase(); try { addAttribute(attributes, key, method.invoke(annotation)); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } } } } return attributes; }
/** * Build the structure hierarchy, first by sorting the structure, by offset and depth, then using * a deque to recursively create the structure. * * @param structures the list of all structural annotations * @return the root structure node */ protected static <T extends Annotation> AnnotationNode<T> build(final List<T> annotations) { Collections.sort( annotations, (s1, s2) -> { int compare = Integer.compare(s1.getBegin(), s2.getBegin()); if (compare == 0) { compare = Integer.compare(s2.getEnd(), s1.getEnd()); } if (compare == 0) { compare = Integer.compare(getDepth(s1), getDepth(s2)); } return compare; }); final AnnotationNode<T> parent = new AnnotationNode<>(null, null); final Deque<AnnotationNode<T>> deque = new ArrayDeque<>(); deque.push(parent); annotations.forEach(s -> build(deque, s)); return parent; }
@Override public Map<String, String> attributes() { if (attributes != null) { return attributes; } attributes = new HashMap<>(); T annotation = getItem(); if (annotation != null) { if (annotation instanceof Structure) { addAttribute(attributes, "class", ((Structure) annotation).getElementClass()); addAttribute(attributes, "id", ((Structure) annotation).getElementId()); } for (Method method : annotation.getClass().getDeclaredMethods()) { String name = method.getName(); if (name.startsWith("get") && method.getParameterTypes().length == 0 && !nonAttributes.contains(name)) { String key = name.substring(3).toLowerCase(); try { addAttribute(attributes, key, method.invoke(annotation)); } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e); } } } } return attributes; }
/** * Build the structure hierarchy, first by sorting the structure, by offset and depth, then using * a deque to recursively create the structure. * * @param structures the list of all structural annotations * @return the root structure node */ protected static <T extends Annotation> AnnotationNode<T> build(final List<T> annotations) { Collections.sort( annotations, (s1, s2) -> { int compare = Integer.compare(s1.getBegin(), s2.getBegin()); if (compare == 0) { compare = Integer.compare(s2.getEnd(), s1.getEnd()); } if (compare == 0) { compare = Integer.compare(getDepth(s1), getDepth(s2)); } return compare; }); final AnnotationNode<T> parent = new AnnotationNode<>(null, null); final Deque<AnnotationNode<T>> deque = new ArrayDeque<>(); deque.push(parent); annotations.forEach(s -> build(deque, s)); return parent; }
/** * Recursively build the structure hierarchy * * @param deque the deque of nodes * @param s the current annotation */ private static <T extends Annotation> void build(Deque<AnnotationNode<T>> deque, T s) { final AnnotationNode<T> parent = getParent(deque, s); final AnnotationNode<T> node = new AnnotationNode<>(parent, s); parent.addChild(node); deque.push(node); }
@Override public String id() { T annotation = getItem(); if (annotation instanceof Structure) { String id = ((Structure) annotation).getElementId(); if (id != null) { return id; } } return ""; }
@Override public String id() { T annotation = getItem(); if (annotation instanceof Structure) { String id = ((Structure) annotation).getElementId(); if (id != null) { return id; } } return ""; }
@Override public List<String> getClasses() { T annotation = getItem(); if (annotation instanceof Structure) { String classes = ((Structure) annotation).getElementClass(); if (StringUtils.isNotBlank(classes)) { return Arrays.asList(classes.split("\\s")) .stream() .map(String::toLowerCase) .collect(Collectors.toList()); } } return ImmutableList.of(); }
@Override public List<String> getClasses() { T annotation = getItem(); if (annotation instanceof Structure) { String classes = ((Structure) annotation).getElementClass(); if (StringUtils.isNotBlank(classes)) { return Arrays.asList(classes.split("\\s")) .stream() .map(String::toLowerCase) .collect(Collectors.toList()); } } return ImmutableList.of(); }