Link to home
Start Free TrialLog in
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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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

ASKER

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?
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.
You will just need to edit and save the files, or you must have syntax highlighting and all that stuff?
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.

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.
JEditorPane would be a good starting point
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>
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);
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--------------");
}
 
> I'm guessing you mean (Style)    rule.getAttribute(a).

sorry, yes thats what i meant

ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hmm, ther must be a way to get the value for ex: #test   font-family
have u tried toString() ?
blush!

tnx