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
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
kristian_gr
Flag of Norway image

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?
Avatar of R7AF
R7AF
Flag of Netherlands image

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.
Avatar of brunoguimaraes
brunoguimaraes
Flag of Brazil image

You will just need to edit and save the files, or you must have syntax highlighting and all that stuff?
Avatar of kristian_gr
kristian_gr
Flag of Norway image

ASKER

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.

Avatar of R7AF
R7AF
Flag of Netherlands image

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.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

JEditorPane would be a good starting point
Avatar of kristian_gr
kristian_gr
Flag of Norway image

ASKER

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>
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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);
Avatar of kristian_gr
kristian_gr
Flag of Norway image

ASKER

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--------------");
}
 
Avatar of Mick Barry
Mick Barry
Flag of Australia image

> 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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of kristian_gr
kristian_gr
Flag of Norway image

ASKER

hmm, ther must be a way to get the value for ex: #test   font-family
Avatar of Mick Barry
Mick Barry
Flag of Australia image

have u tried toString() ?
Avatar of kristian_gr
kristian_gr
Flag of Norway image

ASKER

blush!

tnx
Java
Java

Java is a platform-independent, object-oriented programming language and run-time environment, designed to have as few implementation dependencies as possible such that developers can write one set of code across all platforms using libraries. Most devices will not run Java natively, and require a run-time component to be installed in order to execute a Java program.

102K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo