1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.io.StringWriter;
  6. public class MetadataStripper {
  7. public static void stripMetadata(Component component) {
  8. if (component == null) {
  9. return;
  10. }
  11. // Iterate through all components in the given component
  12. for (Component child : component.getComponents()) {
  13. stripMetadata(child);
  14. }
  15. // Get the component's name
  16. String name = component.getName();
  17. if (name != null) {
  18. System.out.println("Component Name: " + name);
  19. }
  20. // Get the component's class name
  21. String className = component.getClass().getSimpleName();
  22. System.out.println("Component Class: " + className);
  23. // Get the component's text (if applicable)
  24. if (component instanceof TextComponent) {
  25. TextComponent textComponent = (TextComponent) component;
  26. System.out.println("Text: " + textComponent.getText());
  27. } else if (component instanceof Label) {
  28. Label label = (Label) component;
  29. System.out.println("Text: " + label.getText());
  30. }
  31. // Get the component's background color
  32. Color backgroundColor = component.getBackground();
  33. System.out.println("Background Color: " + backgroundColor);
  34. // Get the component's foreground color
  35. Color foregroundColor = component.getForeground();
  36. System.out.println("Foreground Color: " + foregroundColor);
  37. }
  38. //Helper class to make anonymous inner class for testing.
  39. public static class TextComponent extends Component {
  40. private String text;
  41. public TextComponent(String text) {
  42. this.text = text;
  43. }
  44. public String getText() {
  45. return text;
  46. }
  47. }
  48. public static void main(String[] args) {
  49. // Example usage:
  50. Container container = new Container();
  51. TextComponent textField = new TextComponent("Some Text");
  52. container.add(textField);
  53. Label label = new Label("Label Text");
  54. container.add(label);
  55. Button button = new Button("Click Me");
  56. container.add(button);
  57. stripMetadata(container); // Start the metadata stripping process
  58. }
  59. }

Add your comment