Skip to content

Instantly share code, notes, and snippets.

@blad
Created April 29, 2022 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blad/855b7285e59030a7a57eb1fd78dadf90 to your computer and use it in GitHub Desktop.
Save blad/855b7285e59030a7a57eb1fd78dadf90 to your computer and use it in GitHub Desktop.
XML Analyzer
(ns xml-ids.core
(:require [clojure.xml :as xml])
(:require [clojure.string :as string])
(:require [clojure.java.io :as io])
(:gen-class))
(def source-list "list.txt") ;; List of paths to XML layouts, one per line.
(def ignored-tags [:import :data :variable :import :layout :merge :layout])
(defn skip? [tag] (some #(= % tag) ignored-tags))
(defn has-id? [attrs] (contains? attrs :android:id))
(defn identify [root nesting-level]
(let [tag (:tag root)
children (:content root)]
(do
(println "")
(print (str nesting-level ";"))
(print (str tag ";"))
(print (str (count children) ";"))
(print (str (or
(skip? (:tag root))
(has-id? (:attrs root)))
";"))
(doseq [child children
:when (not (skip? (:tag child)))]
(identify child (inc nesting-level)))
)))
(defn -main
[& args]
(let [layouts (string/split (slurp source-list) #"\n") ]
(doseq [layout layouts]
(let [xml-contents (slurp layout)
parsed-xml (->
xml-contents
.getBytes
io/input-stream
xml/parse)]
(println "")
(print layout)
(identify parsed-xml 0)
(println "")))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment