kristian_gr
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
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
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?
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?
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.
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
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.h tml");
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("\tAttr ibute:" + 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>
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.h
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("\tAttr
}
}
}
<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);
shows how to read file, or if using a JEditorPane it'll do the reading for you
2. style.getAttribute(a);
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("object Name:" + ob.getClass().getName());
result: "objectName:javax.swing.te xt.html.CS S$FontFami ly"
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).
I've tried this and it lead me to yet another question:
Object ob = rule.getAttribute(a);
System.out.println("object
result: "objectName:javax.swing.te
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
sorry, yes thats what i meant
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hmm, ther must be a way to get the value for ex: #test font-family
have u tried toString() ?
ASKER
blush!
tnx
tnx
building your own isn't a small job