Avatar of kristian_gr
kristian_grFlag for Norway asked on

Opening, changing and saving a css file

I'm in a bit of a hurry here, so without r.t.f.m, I ask you:
I need to create my own Style Sheet editor.
I have a File (test.css) that I need to open.
Then I need to edit some of the styles, before I save the file.

Any help is appreciated
Best regards
Kristian
Java

Avatar of undefined
Last Comment
kristian_gr

8/22/2022 - Mon
Mick Barry

wouldn't u be better off using an existing one (such as in eclipse)
building your own isn't a small job
ASKER
kristian_gr

I'd preffer not.
My need for this is rather spesial, so one way or another I would need to recreate the gui.
I thought that would be the larges part of the job?
R7AF

Do you need color coding? If not, why don't you use a simple text-editor? Then even a simple textarea element in a html-form could do the trick.
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
brunoguimaraes

You will just need to edit and save the files, or you must have syntax highlighting and all that stuff?
ASKER
kristian_gr

no need for any higlighting or text editor at this point. I'm starting easy.
First and formost, I need to read a localfile (a css file) in a way that I end up with a StyleSheet object.
Then I need to write that StyleSeet object to a new file.

R7AF

So that's basically a normal text file. I don't believe there is anything special about CSS here, well maybe the .css file extension.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mick Barry

JEditorPane would be a good starting point
ASKER
kristian_gr

Hi there, hope you can help me some more here.
Consider the following code, I got two questions:
1.      How can I read a StyleSheet from a local file (File f = new File(c:/test/test.css));
2.      The code prints the name of the style, and the attributes, but who do I get the attribute value?

HTMLEditorKit kit = new HTMLEditorKit();
            HTMLDocument doc = (HTMLDocument) kit.createDefaultDocument();
            FileReader file = new FileReader("C:/test/test.html");
            kit.read(file, doc, 0);
            StyleSheet styles = doc.getStyleSheet();
            Enumeration enu = styles.getStyleNames();
            while (enu.hasMoreElements()) {
                String name = (String) enu.nextElement();
                System.out.println("--------------------------------------");
                System.out.println(" Style name: " + name);
                Style rule = styles.getStyle(name);
                Enumeration enuRule = rule.getAttributeNames();
                while (enuRule.hasMoreElements()) {
                    Object o = (Object) enuRule.nextElement();
                    if (o instanceof CSS.Attribute) {
                        CSS.Attribute a = (CSS.Attribute) o;
                        System.out.println("\tAttribute:" + a.toString());
                    }
                }
            }


<html>
<head>
<style type="text/css">
      <!--
      body {
            background-color: #0033FF;
      }
      #test {
            font-family: Arial, Helvetica, sans-serif;
            background-color: #CCCCCC;
            padding: 15px;
      }
      -->
</style>
</head>
<body>
      <div id="test">test</div>
</body>
</html>
Mick Barry

1. http://www.objects.com.au/java/qa/1052695124.html
shows how to read file, or if using a JEditorPane it'll do the reading for you

2. style.getAttribute(a);
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
kristian_gr

Could not find any (StyleSheet)     styles.getAtribute(a).
I'm guessing you mean (Style)    rule.getAttribute(a).

I've tried this and it lead me to yet another question:
Object ob = rule.getAttribute(a);
System.out.println("objectName:" + ob.getClass().getName());
result: "objectName:javax.swing.text.html.CSS$FontFamily"

CSS$FontFamily <- is the problem. I can't find any CSS.FontFamily.
And the Object is not CSS?

 I'm trying to do something as :
if(ob instanceof CSS) {
     System.out.println("ob = true--------------");
}
 
Mick Barry

> I'm guessing you mean (Style)    rule.getAttribute(a).

sorry, yes thats what i meant

ASKER CERTIFIED SOLUTION
Mick Barry

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
kristian_gr

hmm, ther must be a way to get the value for ex: #test   font-family
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Mick Barry

have u tried toString() ?
ASKER
kristian_gr

blush!

tnx