use of org.w3c.dom.css.CSSStyleDeclaration in project xwiki-platform by xwiki.
the class PdfExportImpl method applyInlineStyle.
/**
* Recursively inline the computed style that applies to a DOM Element into the {@code style} attribute of that
* Element.
*
* @param element the Element whose style should be inlined
*/
private void applyInlineStyle(Element element) {
for (int i = 0; i < element.nodeCount(); i++) {
org.dom4j.Node node = element.node(i);
if (node instanceof CSSStylableElement) {
CSSStylableElement styleElement = (CSSStylableElement) node;
CSSStyleDeclaration style = styleElement.getComputedStyle();
if (style != null && StringUtils.isNotEmpty(style.getCssText())) {
styleElement.addAttribute("style", style.getCssText());
}
}
if (node instanceof Element) {
applyInlineStyle((Element) node);
}
}
}
use of org.w3c.dom.css.CSSStyleDeclaration in project xwiki-platform by xwiki.
the class XWikiWikiModel method getImageDimension.
/**
* Extracts the specified image dimension from the image parameters.
*
* @param dimension either {@code width} or {@code height}
* @param imageParameters the image parameters; may include the {@code width}, {@code height} and {@code style}
* parameters
* @return the value of the passed dimension if it is specified in the image parameters, {@code null} otherwise
*/
private String getImageDimension(String dimension, Map<String, String> imageParameters) {
// Check first if the style parameter contains information about the given dimension. In-line style has priority
// over the dimension parameters.
String value = null;
String style = imageParameters.get("style");
if (StringUtils.isNotBlank(style)) {
try {
CSSStyleDeclaration sd = this.cssParser.parseStyleDeclaration(new InputSource(new StringReader(style)));
value = sd.getPropertyValue(dimension);
} catch (Exception e) {
// Ignore the style parameter but log a warning to let the user know.
this.logger.warn("Failed to parse CSS style [{}]", style);
}
}
if (StringUtils.isBlank(value)) {
// Fall back on the value of the dimension parameter.
value = imageParameters.get(dimension);
}
return value;
}
use of org.w3c.dom.css.CSSStyleDeclaration in project webtools.sourceediting by eclipse.
the class CSSPropertySource method getPropertyDescriptors.
/**
* Returns the current collection of property descriptors.
*
* @return a vector containing all descriptors.
*/
public IPropertyDescriptor[] getPropertyDescriptors() {
time0 = System.currentTimeMillis();
CSSMetaModel metamodel = CSSMetaModelFinder.getInstance().findMetaModelFor(fNode);
Iterator iProperties = Collections.EMPTY_LIST.iterator();
switch(fNode.getNodeType()) {
case ICSSNode.STYLERULE_NODE:
case ICSSNode.FONTFACERULE_NODE:
case ICSSNode.PAGERULE_NODE:
case ICSSNode.STYLEDECLARATION_NODE:
CSSMMNode mmParent = new CSSMetaModelUtil(metamodel).getMetaModelNodeFor(fNode);
if (mmParent != null) {
iProperties = mmParent.getChildNodes();
}
break;
case ICSSNode.STYLEDECLITEM_NODE:
CSSMMNode mmNode = new CSSMetaModelUtil(metamodel).getMetaModelNodeFor(fNode);
if (mmNode != null) {
iProperties = Collections.singletonList(mmNode).iterator();
}
break;
default:
break;
}
// setup categories
Map categories = new HashMap();
Iterator iCategories = metamodel.getCategories();
while (iCategories.hasNext()) {
CSSMMCategory category = (CSSMMCategory) iCategories.next();
categories.put(category.getName(), category.getCaption());
}
// collect property names
Set declaredProperties = new HashSet();
if (iProperties.hasNext()) {
CSSStyleDeclaration declaration = getDeclarationNode();
if (declaration != null) {
ICSSNodeList nodeList = ((ICSSNode) declaration).getChildNodes();
int nProps = (nodeList != null) ? nodeList.getLength() : 0;
for (int i = 0; i < nProps; i++) {
ICSSNode node = nodeList.item(i);
if (node instanceof ICSSStyleDeclItem) {
String name = ((ICSSStyleDeclItem) node).getPropertyName();
if (name != null && 0 < name.length()) {
declaredProperties.add(name.toLowerCase());
}
}
}
}
}
List descriptors = new ArrayList();
// first: properties from content model
while (iProperties.hasNext()) {
CSSMMNode node = (CSSMMNode) iProperties.next();
if (node.getType() == CSSMMNode.TYPE_PROPERTY || node.getType() == CSSMMNode.TYPE_DESCRIPTOR) {
// $NON-NLS-1$
String category = (String) categories.get(node.getAttribute("category"));
String name = node.getName().toLowerCase();
if (declaredProperties.contains(name)) {
declaredProperties.remove(name);
}
IPropertyDescriptor descriptor = createPropertyDescriptor(name, category);
if (descriptor != null) {
descriptors.add(descriptor);
}
}
}
// second: existing properties but not in content model
Iterator iRemains = declaredProperties.iterator();
while (iRemains.hasNext()) {
IPropertyDescriptor descriptor = createPropertyDescriptor((String) iRemains.next(), null);
if (descriptor != null) {
descriptors.add(descriptor);
}
}
IPropertyDescriptor[] resultArray = new IPropertyDescriptor[descriptors.size()];
if (PERF_GETDESCRIPTORS) {
// $NON-NLS-1$ //$NON-NLS-2$
System.out.println(getClass().getName() + ".getPropertyDescriptors: " + (System.currentTimeMillis() - time0) + "ms");
}
return (IPropertyDescriptor[]) descriptors.toArray(resultArray);
}
use of org.w3c.dom.css.CSSStyleDeclaration in project webtools.sourceediting by eclipse.
the class CSSPropertySource method getPropertyValue.
/**
* Returns the current value for the named property.
*
* @param name
* the name of the property as named by its property descriptor
* @return the current value of the property
*/
public Object getPropertyValue(Object name) {
if (name == null) {
// $NON-NLS-1$
return "";
}
String valueString = null;
String nameString = name.toString();
CSSStyleDeclaration declaration = null;
switch(fNode.getNodeType()) {
case ICSSNode.STYLEDECLITEM_NODE:
valueString = ((ICSSStyleDeclItem) fNode).getCSSValueText();
break;
case ICSSNode.STYLERULE_NODE:
case ICSSNode.FONTFACERULE_NODE:
case ICSSNode.PAGERULE_NODE:
declaration = (CSSStyleDeclaration) fNode.getFirstChild();
if (declaration != null) {
valueString = declaration.getPropertyValue(nameString);
}
break;
case ICSSNode.STYLEDECLARATION_NODE:
valueString = ((CSSStyleDeclaration) fNode).getPropertyValue(nameString);
break;
case ICSSNode.PRIMITIVEVALUE_NODE:
ICSSNode parent = fNode;
while (parent != null && !(parent instanceof ICSSStyleDeclItem)) {
parent = parent.getParentNode();
}
if (parent != null) {
valueString = ((ICSSStyleDeclItem) parent).getCSSValueText();
}
break;
default:
break;
}
if (valueString == null) {
// $NON-NLS-1$
valueString = "";
}
return valueString;
}
use of org.w3c.dom.css.CSSStyleDeclaration in project webtools.sourceediting by eclipse.
the class CSSPropertySource method setPropertyValue.
/**
* Sets the named property to the given value.
*
* @param name
* the name of the property being set
* @param value
* the new value for the property
*/
public void setPropertyValue(Object name, Object value) {
if (name == null) {
return;
}
String valueString = (value != null) ? value.toString() : null;
String nameString = name.toString();
CSSStyleDeclaration declaration = getDeclarationNode();
if (declaration != null) {
try {
if (valueString == null || valueString.length() <= 0) {
declaration.removeProperty(nameString);
} else {
String priority = declaration.getPropertyPriority(nameString);
// $NON-NLS-1$ //$NON-NLS-2$
declaration.setProperty(nameString, valueString, (priority == null || priority.length() == 0) ? "" : " " + priority);
}
} catch (Exception e) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
// $NON-NLS-1$
String title = CSSUIMessages.Title_InvalidValue;
// $NON-NLS-1$
String message = CSSUIMessages.Message_InvalidValue;
MessageDialog.openWarning(window.getShell(), title, message);
}
}
}
Aggregations