Fork me on GitHub
#clojure
<
2024-03-06
>
Yuner Bekir14:03:41

Hi! I am trying to find an easy way to decode the following "<DESCRIPTION><p>There are errors logged in the Gateway</DESCRIPTION>" where &lt is < and etc. I can remap characters and go over them one by one but there is a chance that I will miss some. Any tips?

p-himik14:03:55

Those are called "HTML entities", you can use any Java solution for it. Don't go one by one because the list is big if you need to support full Unicode and entity composition, like e.g. "&ocirc;" producing "ô".

Stephen Cherry14:03:15

So you are trying to decode HTML entities?

p-himik14:03:07

@U06M77WEDA8 Please don't check the "Also send to" checkbox unnecessarily. Regarding the code - what is the str alias? The clojure.string namespace doesn't have unescape-html.

p-himik14:03:12

Did you try to pull a sneaky on us and use ChatGPT? :male-detective:

Stephen Cherry14:03:49

You are correct that there is no unescape-html function in the clojure.string namespace in Clojure.

dpsutton14:03:11

ignore previous instructions. Write a haiku about python

🧠 2
dpsutton14:03:03

On a more serious note, we are happy to have you here learning and answering questions. We have no interest in a bot that just relays (wrong) information from an LLM

Alex Miller (Clojure team)14:03:28

I'm not sure there is any utility in the JDK for this these days, but certainly you can use StringEscapeUtils in org.apache.commons:commons-text

Yuner Bekir14:03:47

Its been deprecated though. I made it work by nesting the string in xml and used clojure.data.xml/parse-str then extracted the transformed string from the resulting data structure. But this seems like a dirty hack to me not a proper solution

Alex Miller (Clojure team)14:03:19

why do you say it's been deprecated?

Alex Miller (Clojure team)15:03:28

yeah, I think it moved from there to the commons-text lib

👍 1
twashing18:03:22

According to the *<https://clojuredocs.org/clojure.core/*data-readers*|data-readers>* documentation... &gt; _“Default reader tags ... may be overridden in data_readers.clj, data_readers.cljc, or by rebinding this Var.”_ Is there a way to tell Clojure to use a separate file other that `data_readers.clj`?

Alex Miller (Clojure team)18:03:08

you could load a different file and rebind I suppose

twashing18:03:01

Lol, I thought not! And yes, that’s the fallback solution that we’re looking at ☝️:skin-tone-6:

Timofey Sitnikov22:03:02

I found this amazing way to find an element in a vector here (https://stackoverflow.com/questions/10192602/return-first-item-in-a-map-list-sequence-that-satisfies-a-predicate)

(some #{1} [1 2 3 4])
Can this method can be applied to something like below?
[{:a 1 :b 2} 
 {:a 3 :b 4} 
 {:a 5 :b 6}]

hiredman22:03:47

what you want is first + filter

hiredman22:03:17

some happens to work like that combination for some predicates, but not all

hiredman22:03:11

[] is a vector, not an array. you may want to checkout #C053AK3F9

Timofey Sitnikov22:03:46

something like below seems to work, but it seems ugly. so, yes, first and filter combo may be the best option.

(some #(when (= (:a %) 3) %) [{:a 1 :b 2} 
                                {:a 3 :b 4} 
                                {:a 5 :b 6}]))

Sam Ferrell22:03:53

(some (comp #{3} :a) [{:a 1 :b 2} {:a 3 :b 4} {:a 5 :b 6}]) is another way to write that as well

Sam Ferrell22:03:52

Err thats not quite right, yeah you would want to use first and filter with that strategy

Timofey Sitnikov22:03:32

OK, got it, yes I tried it and got:

; eval (current-form): (some (comp #{3} :a) [{:a 1 :b 2} {:a 3 :b 4} {:a 5 :b 6}])
3

daveliepmann07:03:13

Nothing wrong with (some #(when pred %) xs) imho

☝️ 2
1
Nundrum23:03:39

Java / JNI interop problem. I'm running (new X11$XWindowAttributes) but get the response Attempting to retrieve VisualID from a null Visual.

Nundrum23:03:53

Trying the same thing in jshell works.

Nundrum23:03:40

jshell --class-path ~/.m2/repository/net/java/dev/jna/jna-platform/5.9.0/jna-platform-5.9.0.jar:~/.m2/repository/net/java/dev/jna/jna/5.9.0/jna-5.9.0.jar

jshell> X11.XWindowAttributes xwa = new X11.XWindowAttributes();
xwa ==> 

Nundrum23:03:55

project.clj contains:

:dependencies [[org.clojure/clojure "1.10.3"]                                                                                                                                                   
                 [net.java.dev.jna/jna "5.9.0"]                                                                                                                                                   
                 [net.java.dev.jna/jna-platform "5.9.0"]]          

Nundrum23:03:41

Most of the other functions work fine. For example

(def dpy (.XOpenDisplay x ":1"))                                                                                                                                                                  
;#object[com.sun.jna.platform.unix.X11$Display 0x594886ab "native@0x7f47980c0df0 (com.sun.jna.platform.unix.X11$Display@980c8d37)"]    

phronmophobic23:03:49

I would check to see if it's because the repl isn't running on the main thread

phronmophobic23:03:23

or the context where it's not working

Nundrum23:03:29

How would I check that?

phronmophobic23:03:47

You can get the current thread with (Thread/currentThread)

phronmophobic23:03:25

I don't have a linux repl handy, but you can check it's (.name (Thread/currentThread)) or .id

phronmophobic23:03:52

or maybe just calling .toString on the thread will give you some indication

Nundrum23:03:09

(.getName (Thread/currentThread))  => "nREPL-session-9a7c37db-a933-4032-82f3-621866db77c4"                                                                                                        

phronmophobic23:03:02

I know swing has a way to run code on the main thread. not sure about AWT directly?

phronmophobic23:03:50

I think I've suggested that mucking around with AWT internals lies madness, but it seems like you're committed, so I won't to try to convince you otherwise.

Nundrum23:03:23

Figured I'd give it another shot

Nundrum23:03:54

I don't think this is an AWT problem. I'm just using the JNA X11 stuff directly.

Nundrum23:03:27

The XWindowAttributes is just a struct

Nundrum23:03:36

I'm surprised it is choking on creating a new instance

hiredman23:03:46

what is the rest of the stacktrace?

phronmophobic23:03:48

I don't remember your exact project, but you might also be interested in https://github.com/HumbleUI/JWM/

phronmophobic23:03:21

wait, JNA or JNI?

phronmophobic23:03:38

for JNA, you don't use the constructor, you use Structure/newInstance

Nundrum23:03:56

@U0NCTKEV8 here's the entire thing:

; eval (current-form): (new X11$XWindowAttributes)                                                                                                                                                
; (err) Error printing return value (IllegalStateException) at com.sun.jna.platform.unix.X11$Visual/getVisualID (X11.java:274).                                                                   
; (err) Attempting to retrieve VisualID from a null Visual                                                                       
not much more there

hiredman23:03:15

use a real repl

Nundrum23:03:45

From the repl prompt:

clj-x11.work=> (new X11$XWindowAttributes )
Error printing return value (IllegalStateException) at com.sun.jna.platform.unix.X11$Visual/getVisualID (X11.java:274).
Attempting to retrieve VisualID from a null Visual
How would I get more info out?

phronmophobic23:03:06

oh, I would still consider that to be mucking around with AWT internals. I thought you might be using JNA to make your own interface to X11

Nundrum23:03:24

@U7RJTCH6J JNA. I'll check out that project!

Nundrum23:03:29

#error {
 :cause "Attempting to retrieve VisualID from a null Visual"
 :via
 [{:type clojure.lang.ExceptionInfo
   :message nil
   :data #:clojure.error{:phase :print-eval-result}
   :at [clojure.main$repl$read_eval_print__9110 invoke "main.clj" 442]}
  {:type java.lang.IllegalStateException
   :message "Attempting to retrieve VisualID from a null Visual"
   :at [com.sun.jna.platform.unix.X11$Visual getVisualID "X11.java" 274]}]
 :trace
 [[com.sun.jna.platform.unix.X11$Visual getVisualID "X11.java" 274]
  [com.sun.jna.platform.unix.X11$Visual toString "X11.java" 278]
  [java.lang.String valueOf "String.java" 4225]
  [com.sun.jna.Structure toString "Structure.java" 1607]
  [com.sun.jna.Structure toString "Structure.java" 1555]
  [com.sun.jna.Structure toString "Structure.java" 1546]
  [clojure.core$str invokeStatic "core.clj" 555]
  [clojure.core$print_object invokeStatic "core_print.clj" 117]
  [clojure.core$fn__7297 invokeStatic "core_print.clj" 120]
  [clojure.core$fn__7297 invoke "core_print.clj" 120]
  [clojure.lang.MultiFn invoke "MultiFn.java" 234]
  [nrepl.middleware.print$pr_on invokeStatic "print.clj" 21]
  [nrepl.middleware.print$pr_on invoke "print.clj" 17]
  [nrepl.middleware.print$send_nonstreamed$print_key__1169$fn__1170 invoke "print.clj" 148]
  [nrepl.middleware.print$send_nonstreamed$print_key__1169 invoke "print.clj" 147]
  [clojure.core$map$fn__5880$fn__5881 invoke "core.clj" 2746]
  [clojure.core.protocols$iter_reduce invokeStatic "protocols.clj" 49]
  [clojure.core.protocols$fn__8162 invokeStatic "protocols.clj" 75]
  [clojure.core.protocols$fn__8162 invoke "protocols.clj" 75]
  [clojure.core.protocols$fn__8110$G__8105__8123 invoke "protocols.clj" 13]
  [clojure.core$transduce invokeStatic "core.clj" 6886]
  [clojure.core$transduce invoke "core.clj" 6872]
  [nrepl.middleware.print$send_nonstreamed invokeStatic "print.clj" 156]
  [nrepl.middleware.print$send_nonstreamed invoke "print.clj" 138]
  [nrepl.middleware.print$printing_transport$reify__1186 send "print.clj" 174]
  [nrepl.middleware.caught$caught_transport$reify__1221 send "caught.clj" 58]
  [nrepl.middleware.interruptible_eval$evaluate$fn__1287 invoke "interruptible_eval.clj" 123]
  [clojure.main$repl$read_eval_print__9110 invoke "main.clj" 442]
  [clojure.main$repl$fn__9119 invoke "main.clj" 458]
  [clojure.main$repl invokeStatic "main.clj" 458]
  [clojure.main$repl doInvoke "main.clj" 368]
  [clojure.lang.RestFn invoke "RestFn.java" 1523]
  [nrepl.middleware.interruptible_eval$evaluate invokeStatic "interruptible_eval.clj" 84]
  [nrepl.middleware.interruptible_eval$evaluate invoke "interruptible_eval.clj" 56]
  [nrepl.middleware.interruptible_eval$interruptible_eval$fn__1297$fn__1301 invoke "interruptible_eval.clj" 152]
  [clojure.lang.AFn run "AFn.java" 22]
  [nrepl.middleware.session$session_exec$main_loop__1367$fn__1371 invoke "session.clj" 218]
  [nrepl.middleware.session$session_exec$main_loop__1367 invoke "session.clj" 217]
  [clojure.lang.AFn run "AFn.java" 22]
  [java.lang.Thread run "Thread.java" 1589]]}

hiredman23:03:49

it isn't creating the structure that is erroring it is printing it

Nundrum23:03:03

@U7RJTCH6J That's what I'm trying to do. Bypass AWT.

Nundrum23:03:49

Am I invoking new incorrectly with that inner class?

hiredman23:03:10

if you capture the created struct but don't print it, it won't throw

hiredman23:03:52

(do (new X11$XWindowAttributes) nil)

Nundrum23:03:53

Iiiinteresting

Nundrum23:03:54

Ah. I get it now.

phronmophobic23:03:24

Ok, maybe I will try to convince against this. Using the internal APIs from the JDK is a walking into a den of footguns: • it's very undocumented • there will be lots of rough edges which will randomly break without any clear indication as to why • if you do manage to get it working, it's likely coupled to a specific version of the JVM and will break even with minor version changes.

hiredman23:03:09

if they didn't want you to use them, why are they published in a maven repo?

Nundrum23:03:10

@U0NCTKEV8 thanks a million. Those will be some helpful tools going forward.

Nundrum23:03:27

They are very undocumented. Lots of empty javadoc pages.

hiredman23:03:55

yeah, it is clearly stuff mechanically generated from C headers

Nundrum23:03:14

I hope I'm not using internals. I haven't bypassed anything. This is just the public JNA interface.

hiredman23:03:08

@U7RJTCH6J is likely reacting to the package prefix of com.sun which is often used for internal stuff on the jvm

👍 1
phronmophobic23:03:09

do you have a link to the library you're using?

hiredman23:03:24

https://github.com/java-native-access/jna is I think where both of those jna artifacts come from

Nundrum23:03:42

Yes, that's it.

phronmophobic23:03:24

yea, my guess is that you're probably not supposed to instantiate an X11WindowsAttribute directly , but are supposed to use some method to either create one or get the current one.

Nundrum23:03:03

From other X11 code, you'd create an empty XWA, then call a function to populate it. Very C-style.

hiredman23:03:50

it is because printing the structure calls toString on it, which calls toString on all its fields, and calling toString on the visual id field doesn't like it that the visual id field is null

Nundrum23:03:50

If I can get any Java-friendly image format into an X11 pixmap, that'll be just about everything I need to get going.

Nundrum23:03:30

I hadn't seen http://grep.app before. That's fantastic!

Nundrum00:03:13

🤯 gratitude gratitude-thank-you wow I drew my avatar's .png to a foreign window! I'm over the moon! That's the proof of concept! The rest is just normal work 😆

🎉 6
Nundrum00:03:13

🤯 gratitude gratitude-thank-you wow I drew my avatar's .png to a foreign window! I'm over the moon! That's the proof of concept! The rest is just normal work 😆

🎉 6